Recurse an array with an object/s

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

    Recurse an array with an object/s

    Hello,
    I am using php4
    And I have a class product
    I create an array of productObjects that has 2 dimensions.
    Generraly I am sorting my objects inside arrays and I need to some how
    recurse those arrays to get to the objects.

    To do that I am using the following method recurseArray()

    function recurseArray($a rray) {
    if(is_array($ar ray)) {
    foreach($array as $arrayValue) {
    recurseArray($a rrayValue);
    }
    }
    elseif(is_objec t($array)) {
    $object = $array;
    return $object;
    }
    }

    the problem with that is that when i try to do
    $_product = recurseArray($p roductArray)
    print_r($_produ ct);

    it won't pring anything. But if i do the pring inside the method like
    that
    ....
    elseif(is_objec t($array)) {
    $object = $array;
    print_r($object )
    return $object;
    }
    ....
    it will print my object.

    Do you know why is this happening ?
    It makes me crazy !!!
    Thanks, Angelos.

  • petersprc

    #2
    Re: Recurse an array with an object/s

    Hi,

    Please try: return recurseArray($a rrayValue);

    Should do the trick.

    On Feb 7, 9:49 am, "Aggelos" <djje...@gmail. comwrote:
    Hello,
    I am using php4
    And I have a class product
    I create an array of productObjects that has 2 dimensions.
    Generraly I am sorting my objects inside arrays and I need to some how
    recurse those arrays to get to the objects.
    >
    To do that I am using the following method recurseArray()
    >
    function recurseArray($a rray) {
    if(is_array($ar ray)) {
    foreach($array as $arrayValue) {
    recurseArray($a rrayValue);
    }
    }
    elseif(is_objec t($array)) {
    $object = $array;
    return $object;
    }
    >
    }
    >
    the problem with that is that when i try to do
    $_product = recurseArray($p roductArray)
    print_r($_produ ct);
    >
    it won't pring anything. But if i do the pring inside the method like
    that
    ...
    elseif(is_objec t($array)) {
    $object = $array;
    print_r($object )
    return $object;
    }
    ...
    it will print my object.
    >
    Do you know why is this happening ?
    It makes me crazy !!!
    Thanks, Angelos.

    Comment

    • Rik

      #3
      Re: Recurse an array with an object/s

      Aggelos <djjelly@gmail. comwrote:
      Hello,
      I am using php4
      And I have a class product
      I create an array of productObjects that has 2 dimensions.
      Generraly I am sorting my objects inside arrays and I need to some how
      recurse those arrays to get to the objects.
      >
      To do that I am using the following method recurseArray()
      >
      function recurseArray($a rray) {
      if(is_array($ar ray)) {
      foreach($array as $arrayValue) {
      recurseArray($a rrayValue);
      This won't do anything, you returned objects are cast in the void....

      How would you like your objects returned by the function? In case of a
      flat array (which I suspect):


      function objects_from_ar ray($var){
      $return = array();
      if(is_object($v ar)){
      $return[] = $var;
      }
      if(is_array($va r)){
      $return = array_merge($re turn,objects_fr om_array($var)) ;
      }
      return $return;
      }
      --
      Rik Wasmus

      Comment

      • Aggelos

        #4
        Re: Recurse an array with an object/s

        On Feb 7, 2:53 pm, "petersprc" <peters...@gmai l.comwrote:
        Hi,
        >
        Please try: return recurseArray($a rrayValue);
        >
        That would just loop forever... won't it ?

        Comment

        • Aggelos

          #5
          Re: Recurse an array with an object/s

          function objects_from_ar ray($var){
          $return = array();
          if(is_object($v ar)){
          $return[] = $var;
          }
          if(is_array($va r)){
          $return = array_merge($re turn,objects_fr om_array($var)) ;
          }
          return $return;}
          >
          That function will also enter an endless loop like petesprc
          We need to loop inside the array to get the next dimension... don't
          we ? so when you do arraymerge the $var should be the second dimension
          of the array in the first call of the function.

          Isn't that right ?
          So I suppose I need to have a foreach loop

          my array is like that
          $productArray[$productTypeId][$productId][$published] = new
          objectProduct() ;

          so for that i would do 3 nested foreach loops ...
          But I want to do that using a function to get to the objectProduct.

          Comment

          • Rik

            #6
            Re: Recurse an array with an object/s

            Aggelos <djjelly@gmail. comwrote:
            >function objects_from_ar ray($var){
            > $return = array();
            > if(is_object($v ar)){
            > $return[] = $var;
            > }
            > if(is_array($va r)){
            > $return = array_merge($re turn,objects_fr om_array($var)) ;
            Oops,
            if(is_array($va r){
            foreach($var as $item){
            $return = array_merge($re turn, objects_from_ar ray($item));
            }
            }
            > }
            > return $return;}
            >>
            That function will also enter an endless loop like petesprc
            No, it won't like this :P. It will loop through an array and if it's done,
            it will return all objects found. Unless you're doing something very
            strange like putting in a reference to a parent array in a sub array.

            We need to loop inside the array to get the next dimension... don't
            we ? so when you do arraymerge the $var should be the second dimension
            of the array in the first call of the function.
            my array is like that
            $productArray[$productTypeId][$productId][$published] = new
            objectProduct() ;
            >
            so for that i would do 3 nested foreach loops ...
            Or just a recursive function.

            --
            Rik Wasmus

            Comment

            • Aggelos

              #7
              Re: Recurse an array with an object/s

              function objects_from_ar ray($var){
              $return = array();
              if(is_object($v ar)){
              $return = $var;
              }
              if(is_array($va r)){
              foreach($var as $item){
              $return = array_merge($re turn, objects_from_ar ray($item));

              }
              }
              return $return;
              }

              Ok that will work now but it is not what I want because at the end my
              object gets converted into an assiciative array.
              So my product->title now would become product[;title']

              Do you know why my version isn't working ? you said something about
              void... but i didn't understood that...

              Comment

              • Rik

                #8
                Re: Recurse an array with an object/s

                Aggelos <djjelly@gmail. comwrote:
                function objects_from_ar ray($var){
                $return = array();
                if(is_object($v ar)){
                $return = $var;
                }
                if(is_array($va r)){
                foreach($var as $item){
                $return = array_merge($re turn, objects_from_ar ray($item));
                >
                }
                }
                return $return;
                }
                >
                Ok that will work now but it is not what I want because at the end my
                object gets converted into an assiciative array.
                Not in my code, I assume in something you want to do afterwards? I told
                you I did not know in which format you want the return.
                So my product->title now would become product[;title']
                Euh? That's something entirely different. Please explain your exact
                intentions from start to finish, would make it a lot easier to give you an
                answer you can actually use :-)
                Do you know why my version isn't working ? you said something about
                void... but i didn't understood that...
                This part:
                function recurseArray($a rray) {
                if(is_array($ar ray)) {
                foreach($array as $arrayValue) {
                recurseArray($a rrayValue);
                -----------------------^^

                If the $array you feed it is an object, the same object is immediately
                returned. So far so good. If it is an array, you perform the function on
                each item of the array. This may or may not return an object. However,
                there's nothing 'to the left' of this recurseArray($a rrayValue), which
                means that whatever it returns, it is not assigned to anything, so
                whatever it returns is lost. If you feed it an array, the function is
                performed on every item, but isn't 'saved'. The only 'return' there is for
                when it's an object, so if it's not an object your function will return
                nothing/null/nada.
                --
                Rik Wasmus

                Comment

                • Aggelos

                  #9
                  Re: Recurse an array with an object/s

                  On Feb 7, 4:25 pm, Rik <luiheidsgoe... @hotmail.comwro te:
                  Aggelos <djje...@gmail. comwrote:
                  function objects_from_ar ray($var){
                  $return = array();
                  if(is_object($v ar)){
                  $return = $var;
                  }
                  if(is_array($va r)){
                  foreach($var as $item){
                  $return = array_merge($re turn, objects_from_ar ray($item));
                  >
                  }
                  }
                  return $return;
                  }
                  >
                  Ok that will work now but it is not what I want because at the end my
                  object gets converted into an assiciative array.
                  >
                  Not in my code, I assume in something you want to do afterwards? I told
                  you I did not know in which format you want the return.
                  >
                  So my product->title now would become product[;title']
                  >
                  Euh? That's something entirely different. Please explain your exact
                  intentions from start to finish, would make it a lot easier to give you an
                  answer you can actually use :-)
                  >
                  Do you know why my version isn't working ? you said something about
                  void... but i didn't understood that...
                  >
                  This part:
                  function recurseArray($a rray) {
                  if(is_array($ar ray)) {
                  foreach($array as $arrayValue) {
                  recurseArray($a rrayValue);
                  -----------------------^^
                  >
                  If the $array you feed it is an object, the same object is immediately
                  returned. So far so good. If it is an array, you perform the function on
                  each item of the array. This may or may not return an object. However,
                  there's nothing 'to the left' of this recurseArray($a rrayValue), which
                  means that whatever it returns, it is not assigned to anything, so
                  whatever it returns is lost. If you feed it an array, the function is
                  performed on every item, but isn't 'saved'. The only 'return' there is for
                  when it's an object, so if it's not an object your function will return
                  nothing/null/nada.
                  --
                  Rik Wasmus
                  Comprende, I understand now :)
                  Thanks a lot.

                  Comment

                  • Jerry Stuckle

                    #10
                    Re: Recurse an array with an object/s

                    Aggelos wrote:
                    On Feb 7, 2:53 pm, "petersprc" <peters...@gmai l.comwrote:
                    >Hi,
                    >>
                    >Please try: return recurseArray($a rrayValue);
                    >>
                    >
                    That would just loop forever... won't it ?
                    >
                    Not unless you have a loop in your array itself.

                    Your problem is you're only returning something if the last item in the
                    first level of your array is an object. Otherwise you're not returning
                    anything - so print_r has nothing to print.

                    But one question - you say you have a two dimensional array. But this
                    is only a single dimensional array. It just happens that an array
                    element might itself be an array. That's not two dimensions.

                    Why not try posting some sample data and what you're trying to get out
                    of it? Then maybe we could help you a little better.

                    --
                    =============== ===
                    Remove the "x" from my email address
                    Jerry Stuckle
                    JDS Computer Training Corp.
                    jstucklex@attgl obal.net
                    =============== ===

                    Comment

                    • Aggelos

                      #11
                      Re: Recurse an array with an object/s

                      On Feb 7, 4:25 pm, Rik <luiheidsgoe... @hotmail.comwro te:
                      Aggelos <djje...@gmail. comwrote:
                      function objects_from_ar ray($var){
                      $return = array();
                      if(is_object($v ar)){
                      $return = $var;
                      }
                      if(is_array($va r)){
                      foreach($var as $item){
                      $return = array_merge($re turn, objects_from_ar ray($item));
                      >
                      }
                      }
                      return $return;
                      }
                      >
                      Ok that will work now but it is not what I want because at the end my
                      object gets converted into an assiciative array.
                      >
                      Not in my code, I assume in something you want to do afterwards? I told
                      you I did not know in which format you want the return.
                      >
                      So my product->title now would become product[;title']
                      >
                      Euh? That's something entirely different. Please explain your exact
                      intentions from start to finish, would make it a lot easier to give you an
                      answer you can actually use :-)
                      >
                      Do you know why my version isn't working ? you said something about
                      void... but i didn't understood that...
                      >
                      This part:
                      function recurseArray($a rray) {
                      if(is_array($ar ray)) {
                      foreach($array as $arrayValue) {
                      recurseArray($a rrayValue);
                      -----------------------^^
                      >
                      If the $array you feed it is an object, the same object is immediately
                      returned. So far so good. If it is an array, you perform the function on
                      each item of the array. This may or may not return an object. However,
                      there's nothing 'to the left' of this recurseArray($a rrayValue), which
                      means that whatever it returns, it is not assigned to anything, so
                      whatever it returns is lost. If you feed it an array, the function is
                      performed on every item, but isn't 'saved'. The only 'return' there is for
                      when it's an object, so if it's not an object your function will return
                      nothing/null/nada.
                      --
                      Rik Wasmus
                      This is my suggested sollution:
                      It succesfully returns the object ... ofcourse you have to initialise
                      the tempArray but I wasn't very sure about it ...

                      function recurseArray($a rray) {
                      if(is_array($ar ray)) {
                      foreach($array as $arrayValue) {
                      $tempArray = recurseArray($a rrayValue);
                      }
                      }
                      elseif(is_objec t($array)) {
                      return $array;
                      }
                      return $tempArray;
                      }

                      Thanks a lot for your replies.

                      Comment

                      • Rik

                        #12
                        Re: Recurse an array with an object/s

                        Aggelos <djjelly@gmail. comwrote:
                        This is my suggested sollution:
                        It succesfully returns the object ... ofcourse you have to initialise
                        the tempArray but I wasn't very sure about it ...
                        >
                        function recurseArray($a rray) {
                        if(is_array($ar ray)) {
                        foreach($array as $arrayValue) {
                        $tempArray = recurseArray($a rrayValue);
                        }
                        }
                        elseif(is_objec t($array)) {
                        return $array;
                        }
                        return $tempArray;
                        }

                        This will only return the _last_ object found, as you continue to
                        overwrite $tempArray for every array-element.

                        --
                        Rik Wasmus

                        Comment

                        • Aggelos

                          #13
                          Re: Recurse an array with an object/s

                          But one question - you say you have a two dimensional array. But this
                          is only a single dimensional array. It just happens that an array
                          element might itself be an array. That's not two dimensions.
                          ok I'll explain to you and tell me if I am wrong.

                          I have class package and class product

                          A package has many products when I call my constructor for the package
                          I create an array of it's products as well.

                          CODE:

                          class package {
                          var $packageId;
                          var $title;
                          var $description;

                          var $_productArray = array();

                          function package($packag eId=NULL,$title =NULL,$descript ion=NULL) {
                          $this->packageId = $packageId;
                          $this->title = $title;
                          $this->description = $description;

                          $productPackage Array = product_package ::select(array( 'package_id'=>
                          $packageId));
                          foreach($produc tPackageArray[$packageId] as $contentId =>
                          $publishedArray ) {
                          foreach($publis hedArray as $published =$productValue) {
                          $_ObjProduct = product::select (array('content _id'=>
                          $contentId,'pub lished'=>$publi shed));
                          $this->_productArra y[$_product[$contentId][$published]-
                          >productTypeI d] = $_ObjProduct;
                          }
                          }
                          }
                          .......
                          }

                          END OF CODE

                          I am not sure if you'll understand my code but anyway I end up
                          creating product packages and having an object package which instead
                          of having just the ids of the products has the whole object in it.

                          and that creates an array like that $package[$packageId]-
                          >_productArra y[$productTypeId] = $_ObjProduct; (I don't know how to
                          symbolize the object inside the array)

                          So how many dimensions is that and how does it look to you Jerry. ?


                          Comment

                          • Jerry Stuckle

                            #14
                            Re: Recurse an array with an object/s

                            Aggelos wrote:
                            >But one question - you say you have a two dimensional array. But this
                            >is only a single dimensional array. It just happens that an array
                            >element might itself be an array. That's not two dimensions.
                            >
                            ok I'll explain to you and tell me if I am wrong.
                            >
                            I have class package and class product
                            >
                            A package has many products when I call my constructor for the package
                            I create an array of it's products as well.
                            >
                            CODE:
                            >
                            class package {
                            var $packageId;
                            var $title;
                            var $description;
                            >
                            var $_productArray = array();
                            >
                            function package($packag eId=NULL,$title =NULL,$descript ion=NULL) {
                            $this->packageId = $packageId;
                            $this->title = $title;
                            $this->description = $description;
                            >
                            $productPackage Array = product_package ::select(array( 'package_id'=>
                            $packageId));
                            foreach($produc tPackageArray[$packageId] as $contentId =>
                            $publishedArray ) {
                            foreach($publis hedArray as $published =$productValue) {
                            $_ObjProduct = product::select (array('content _id'=>
                            $contentId,'pub lished'=>$publi shed));
                            $this->_productArra y[$_product[$contentId][$published]-
                            >productTypeI d] = $_ObjProduct;
                            }
                            }
                            }
                            ......
                            }
                            >
                            END OF CODE
                            >
                            I am not sure if you'll understand my code but anyway I end up
                            creating product packages and having an object package which instead
                            of having just the ids of the products has the whole object in it.
                            >
                            and that creates an array like that $package[$packageId]-
                            >_productArra y[$productTypeId] = $_ObjProduct; (I don't know how to
                            symbolize the object inside the array)
                            >
                            So how many dimensions is that and how does it look to you Jerry. ?
                            >
                            >
                            Aggelos,

                            This is still a single-dimensional array. Two dimensional arrays are
                            access by two subscripts, i.e. $myarray[1][2].

                            And yes, I understand your code. But again - what does the *data* look
                            like, and what do you expect for output?

                            Try populating your array and print it with print_r(). Post it along
                            with what you would like the output to look like.


                            --
                            =============== ===
                            Remove the "x" from my email address
                            Jerry Stuckle
                            JDS Computer Training Corp.
                            jstucklex@attgl obal.net
                            =============== ===

                            Comment

                            • Jerry Stuckle

                              #15
                              Re: Recurse an array with an object/s

                              Jerry Stuckle wrote:
                              Aggelos wrote:
                              >>But one question - you say you have a two dimensional array. But this
                              >>is only a single dimensional array. It just happens that an array
                              >>element might itself be an array. That's not two dimensions.
                              >>
                              >ok I'll explain to you and tell me if I am wrong.
                              >>
                              >I have class package and class product
                              >>
                              >A package has many products when I call my constructor for the package
                              >I create an array of it's products as well.
                              >>
                              >CODE:
                              >>
                              >class package {
                              > var $packageId;
                              > var $title;
                              > var $description;
                              >>
                              > var $_productArray = array();
                              >>
                              > function package($packag eId=NULL,$title =NULL,$descript ion=NULL) {
                              > $this->packageId = $packageId;
                              > $this->title = $title;
                              > $this->description = $description;
                              >>
                              > $productPackage Array =
                              >product_packag e::select(array ('package_id'=>
                              >$packageId)) ;
                              > foreach($produc tPackageArray[$packageId] as $contentId =>
                              >$publishedArra y) {
                              > foreach($publis hedArray as $published =$productValue) {
                              > $_ObjProduct = product::select (array('content _id'=>
                              >$contentId,'pu blished'=>$publ ished));
                              > $this->_productArra y[$_product[$contentId][$published]-
                              >>productType Id] = $_ObjProduct;
                              > }
                              > }
                              > }
                              >......
                              >}
                              >>
                              >END OF CODE
                              >>
                              >I am not sure if you'll understand my code but anyway I end up
                              >creating product packages and having an object package which instead
                              >of having just the ids of the products has the whole object in it.
                              >>
                              >and that creates an array like that $package[$packageId]-
                              >>_productArr ay[$productTypeId] = $_ObjProduct; (I don't know how to
                              >symbolize the object inside the array)
                              >>
                              >So how many dimensions is that and how does it look to you Jerry. ?
                              >>
                              >>
                              >
                              Aggelos,
                              >
                              This is still a single-dimensional array. Two dimensional arrays are
                              access by two subscripts, i.e. $myarray[1][2].
                              >
                              And yes, I understand your code. But again - what does the *data* look
                              like, and what do you expect for output?
                              >
                              Try populating your array and print it with print_r(). Post it along
                              with what you would like the output to look like.
                              >
                              >
                              Actually, allow me to correct one thing. This is an N-dimensional array
                              (depending on the depth). But you are handling it as a single
                              dimensional array in your recursive function. This is perfectly fine,
                              and I do similar things quite regularly.

                              --
                              =============== ===
                              Remove the "x" from my email address
                              Jerry Stuckle
                              JDS Computer Training Corp.
                              jstucklex@attgl obal.net
                              =============== ===

                              Comment

                              Working...