array of...array....

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bob Bedford

    array of...array....

    I've this structure in a XML file:
    Type
    Model
    Detail
    Datas
    Datas
    Datas
    Datas
    /Detail
    Detail
    Datas
    Datas
    Datas
    /Detail
    /Model
    /Type

    I've parsed all the stuff, by putting everything in an array.

    Now, I've a problem as I must use datas in "Detail" tag every time they are
    processed, but I must first process datas in Model, so I have to wait until
    Model tag is closed.

    For now I put everything in an array called $datas (where I store the couple
    key/value). How can I do to create, at a certain point, a new array for
    storing the Detail array (in order to have an array for every Detail that
    may occur, as this isn't a fixed number of Details ?)

    Thanks for helping.


  • Garp

    #2
    Re: array of...array....


    "Bob Bedford" <bedford1@YouKn owWhatToDoHereh otmail.com> wrote in message
    news:40e05cbf$0 $26629$5402220f @news.sunrise.c h...[color=blue]
    > I've this structure in a XML file:
    > Type
    > Model
    > Detail
    > Datas
    > Datas
    > Datas
    > Datas
    > /Detail
    > Detail
    > Datas
    > Datas
    > Datas
    > /Detail
    > /Model
    > /Type
    >
    > I've parsed all the stuff, by putting everything in an array.
    >
    > Now, I've a problem as I must use datas in "Detail" tag every time they[/color]
    are[color=blue]
    > processed, but I must first process datas in Model, so I have to wait[/color]
    until[color=blue]
    > Model tag is closed.
    >
    > For now I put everything in an array called $datas (where I store the[/color]
    couple[color=blue]
    > key/value). How can I do to create, at a certain point, a new array for
    > storing the Detail array (in order to have an array for every Detail that
    > may occur, as this isn't a fixed number of Details ?)
    >
    > Thanks for helping.[/color]

    Been to http://uk2.php.net/manual/en/ref.xml.php yet? XML parsers are your
    friend.

    Garp


    Comment

    • Savut

      #3
      Re: array of...array....

      Just create a new array for an array data :

      like this

      $data["Model1"]["Detail1"] = array("Datas1", "Datas2", "Datas3");

      Savut

      "Bob Bedford" <bedford1@YouKn owWhatToDoHereh otmail.com> wrote in message
      news:40e05cbf$0 $26629$5402220f @news.sunrise.c h...[color=blue]
      > I've this structure in a XML file:
      > Type
      > Model
      > Detail
      > Datas
      > Datas
      > Datas
      > Datas
      > /Detail
      > Detail
      > Datas
      > Datas
      > Datas
      > /Detail
      > /Model
      > /Type
      >
      > I've parsed all the stuff, by putting everything in an array.
      >
      > Now, I've a problem as I must use datas in "Detail" tag every time they
      > are
      > processed, but I must first process datas in Model, so I have to wait
      > until
      > Model tag is closed.
      >
      > For now I put everything in an array called $datas (where I store the
      > couple
      > key/value). How can I do to create, at a certain point, a new array for
      > storing the Detail array (in order to have an array for every Detail that
      > may occur, as this isn't a fixed number of Details ?)
      >
      > Thanks for helping.
      >
      >[/color]

      Comment

      • Bob Bedford

        #4
        Re: array of...array....

        > Been to http://uk2.php.net/manual/en/ref.xml.php yet? XML parsers are your[color=blue]
        > friend.[/color]

        It's what I've done until now. My message tell that I've already passed this
        stage. I have already parsed the XML file (with the help of examples from
        php.net). The problem comes while using arrays.

        Thanks anyway for your help.


        Comment

        • Bob Bedford

          #5
          Re: array of...array....

          "Savut" <webki@hotmail. com> a écrit dans le message de
          news:wBZDc.35$g y2.866@news20.b ellglobal.com.. .[color=blue]
          > Just create a new array for an array data :
          >
          > like this
          >
          > $data["Model1"]["Detail1"] = array("Datas1", "Datas2", "Datas3");
          >
          > Savut[/color]
          Thanks, Savut, exactly the help I wanted !

          One more question:
          As you can guess, I'm not so confortable with arrays....so, how can I then
          show all datas since sometimes Detail1 can be a value and other it can be an
          array:

          Like in pseudo-code
          for every value of $data
          if $data[value] is an array
          for every Avalue of $data[value]
          echo("$data[value][AValue][Key] -
          $data[value][AValue][Value]<br>");
          else
          echo(echo("$dat a[value][Key] - $data[value][Value]<br>");

          How can I do this ?

          BoB


          Comment

          • Savut

            #6
            Re: array of...array....

            here come is_array($data[x][y]);

            Savut


            "Bob Bedford" <bedford1@YouKn owWhatToDoHereh otmail.com> wrote in message
            news:40e075d5$0 $26648$5402220f @news.sunrise.c h...[color=blue]
            > "Savut" <webki@hotmail. com> a écrit dans le message de
            > news:wBZDc.35$g y2.866@news20.b ellglobal.com.. .[color=green]
            >> Just create a new array for an array data :
            >>
            >> like this
            >>
            >> $data["Model1"]["Detail1"] = array("Datas1", "Datas2", "Datas3");
            >>
            >> Savut[/color]
            > Thanks, Savut, exactly the help I wanted !
            >
            > One more question:
            > As you can guess, I'm not so confortable with arrays....so, how can I then
            > show all datas since sometimes Detail1 can be a value and other it can be
            > an
            > array:
            >
            > Like in pseudo-code
            > for every value of $data
            > if $data[value] is an array
            > for every Avalue of $data[value]
            > echo("$data[value][AValue][Key] -
            > $data[value][AValue][Value]<br>");
            > else
            > echo(echo("$dat a[value][Key] - $data[value][Value]<br>");
            >
            > How can I do this ?
            >
            > BoB
            >
            >[/color]

            Comment

            • Bob Bedford

              #7
              Re: array of...array....

              Thanks Savut,

              In fact I'm stuck before this. I'll tell you the tree and then maybe you can
              help me:
              Take back the example:
              /*************** ***********/
              Type
              SomeDatas
              SomeDatas
              Model (array)
              Mod0Datas
              Mod1Datas
              Detail (array)
              Datas
              Datas
              Datas
              Datas
              /Detail
              Detail (array)
              Datas
              Datas
              Datas
              /Detail
              /Model
              Brand (array)
              Brand0Datas
              Brand1Datas
              Detail (array)
              Datas
              Datas
              /Detail
              Detail (array)
              Datas
              Datas
              Datas
              /Detail
              /Brand
              /Type
              /*************** ***********/
              So I create the $data array for storing everyting in Type.
              function startElement($p arser, $name, $attrs)
              ....
              switch($name){
              case 'Type':
              $datas["TAG"] = $name;
              break;
              case 'Model': //or 'Brand'
              $actualarray = $name;
              $datas[$actualarray] = array(); //create an array for Model or Brand
              elements
              break;
              case 'Datas':
              $datasindex = count($datas[$actualarray]);
              $datas[$actualarray][$datasindex] = $name;

              here I'm stuck !!!!
              - $datas is the "Mother" array, that stores everything
              - when I encounter the word "Model" or "Brand" in the XML file, I've to
              create an array (empty for now).
              - Until I encounter the closing /Model or /Brand, I've to put everything in
              the $datas["Model"] array as long as they are datas.
              in the $datas["Model"] or $datas["Brand"] array, I've to create an other
              array as soon as I encounter the word "Detail" for storing the Detail's
              datas, and then fill the $datas["Model"]["Detail"] until I encounter the
              /Detail word.

              My question is: How do I create such arrays on the fly (look the
              startElement for what I mean) and what should I keep for pointers...I've to
              keep indexes, as I will create an array of array of array, and I will then
              be able to walk trough each element. (this code would also be greately
              appreciated...)
              I've never worked with PHP arrays before, this is the first time I need
              them, and it's quite difficult as a starting point !!!!

              Thank you !!!!

              Best regards.

              BoB


              Comment

              • Garp

                #8
                Re: array of...array....


                "Bob Bedford" <bedford1@YouKn owWhatToDoHereh otmail.com> wrote in message
                news:40e074d1$0 $26647$5402220f @news.sunrise.c h...[color=blue][color=green]
                > > Been to http://uk2.php.net/manual/en/ref.xml.php yet? XML parsers are[/color][/color]
                your[color=blue][color=green]
                > > friend.[/color]
                >
                > It's what I've done until now. My message tell that I've already passed[/color]
                this[color=blue]
                > stage. I have already parsed the XML file (with the help of examples from
                > php.net). The problem comes while using arrays.
                >
                > Thanks anyway for your help.[/color]

                Ah, I didn't know that's what you meant by parsed. Glad you got the help you
                wanted in the end though.

                Garp


                Comment

                • Savut

                  #9
                  Re: array of...array....

                  Type
                  SomeDatas
                  SomeDatas
                  Model (array)
                  Mod0Datas
                  Mod1Datas
                  Detail (array)
                  Datas
                  Datas
                  Datas
                  Datas
                  /Detail
                  Detail (array)
                  Datas
                  Datas
                  Datas
                  /Detail
                  /Model
                  Brand (array)
                  Brand0Datas
                  Brand1Datas
                  Detail (array) --> $BrandDetail[0]
                  Datas
                  Datas
                  /Detail
                  Detail (array) --> $BrandDetail[1]
                  Datas
                  Datas
                  Datas
                  /Detail
                  /Brand
                  /Type


                  I suggest you to use xpath so you can navigate inside an xml document more
                  easily :
                  Here a simple preview of what he can do.
                  $domxml = new DomDocument("yo urxmlfile.xml") ;
                  $xpath = new DomXPath($domxm l);
                  $BrandDetail = $xpath->query("/Type/Brand/Detail");
                  foreach($BrandD etail as $value) {
                  $datas["Brand"]["Detail"][] = $value; //[] is used for auto increment
                  the array
                  }

                  But this work only on PHP5 as I know
                  here some exemple http://www.zend.com/php5/articles/php5-xmlphp.php

                  If not, then use domxml then navigate inside with has_child_nodes () and
                  node_name() and node_value() all with foreach() loop.
                  You can check this for exemple and tutorial, http://ca3.php.net/domxml.

                  This is very long but worth to learn it.

                  Hope this help

                  Savut



                  Comment

                  Working...