5

I have looked through a few answers on here but that don't seem to utilise this method?

I have an array of items, and the items are objects. The object can have a key which is 'children' and 'children' is an array of objects etc.

Is there a way to achieve this?

Example:

    Array
    (
        [1] => stdClass Object
            (
                [id] => 1
                [name] => Steve King
                [image] => upload/shop/fe7a66254e4249af2b0093efca75a914.jpg
                [parent] => 0
                [children] => Array
                    (
                    )

            )

        [2] => stdClass Object
            (
                [id] => 2
                [name] => Eden Hall
                [image] => upload/shop/064f60a98deba612e437ac549f1dc05d.jpg
                [parent] => 0
                [children] =>Array
                    (
                        [1] => stdClass Object
                            (
                              [id] => 1
                              [name] => Steve King
                              [image] => upload/shop/fe7a66254e4249af2b0093efca75a914.jpg
                              [parent] => 0
                              [children] => Array
                                  (
                                  )

                   )
            )
        [3] => stdClass Object
            (
                [id] => 3
                [name] => Paula Johnson
                [image] => upload/shop/1492a323090afbad07c35cf93fe6bdda.jpg
                [parent] => 0
                [children] => Array
                    (
                    )

            )

        [4] => stdClass Object
            (
                [id] => 4
                [name] => Ethan Watson
                [image] => upload/shop/677c720333af33bc58d0684d79918e03.jpg
                [parent] => 0
                [children] => Array
                    (
                    )

            )

        [5] => stdClass Object
            (
                [id] => 5
                [name] => Abigail Adams
                [image] => upload/shop/da1734277322fc3b2e84a9ddbcc2e2f1.jpg
                [parent] => 0
                [children] => Array
                    (
                    )

            )
Sahil Gulati
  • 15,028
  • 4
  • 24
  • 42
Ryan Hipkiss
  • 648
  • 1
  • 7
  • 20

2 Answers2

6

Assign an array of objects under $array.

Solution 1: json_encode an array of objects to make it a json and then converting an json into associative array.

$result=json_decode(json_encode($array),true);
array_walk_recursive($result, function($value,$key){
    print_r($value);
    print_r($key);
});

Solution 2: Iterating over array and type casting each object as array.

array_walk($array,function(&$value,$key){
    $value=(array) $value;
});
array_walk_recursive($array, function($value,$key){
    print_r($value);
    print_r($key);
});
Sahil Gulati
  • 15,028
  • 4
  • 24
  • 42
  • Thank you. I guess it's not possible to loop through while keeping them as objects then? – Ryan Hipkiss Apr 18 '17 at 14:35
  • @RyanHipkiss array_walk_recursive will work for only arrays not objects. – Sahil Gulati Apr 18 '17 at 14:35
  • Thanks, this solution does work. I was hoping that because the 'children' property was an array that it would work. No worries though. – Ryan Hipkiss Apr 18 '17 at 14:39
  • welcome @RyanHipkiss thanks for accepting ... If you have some nested array which contains nested objects as well then use first solution.. It will for for that as well.. – Sahil Gulati Apr 18 '17 at 14:41
  • hi @SahilGulati solution 1 worked for me but now i can't see the hierarchy to my values, i just see key and the value in an unorganized way, anyway I can organize this? – Amir Khadem Dec 22 '20 at 19:31
  • @AmirKhadem Brother unfortunately `array_walk_recursive` itself go nested inside array values and we can't recognize whether its nested key/value or not. Instead you can use 3rd argument `$userData` of array_walk_recursive to find a work around for this. [array_walk_recursive](https://www.php.net/manual/en/function.array-walk-recursive.php). Happy coding :) – Sahil Gulati Dec 23 '20 at 05:19
5

You always can implement a custom recursive iterator for certain data structure. It can be a more flexible solution. For example:

class MyIterator extends \IteratorIterator implements 
\RecursiveIterator
{
    public function hasChildren()
    {
        $current = $this->current();

        if (is_array($current) and $this->key() === 'children') {
            return true;
        }

        return is_object($current);
    }

    public function getChildren()
    {
        /* $current is array (for key 'children') or \stdClass obj*/

        $current = $this->current();

        return new MyIterator(new \ArrayObject($current));
    }
}

$rii = new \RecursiveIteratorIterator(new MyIterator(new 
\ArrayObject($data)));

foreach ($rii as $key => $value) {
    print_r($key);
    print_r($value);
}
Stanislav Poslavsky
  • 2,398
  • 2
  • 24
  • 36
Grigoriy I
  • 81
  • 4