使用array_walk_recursive()为stdClass对象

时间:2017-04-18 14:16:28

标签: php multidimensional-array array-walk

我在这里看了几个答案,但似乎没有使用这种方法?

我有一系列项目,项目是对象。对象可以有一个键,即“孩子”。和孩子们'是一组对象等。

有没有办法实现这个目标?

示例:

    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
                    (
                    )

            )

2 个答案:

答案 0 :(得分:3)

# begin .set_env HELLO="WORLD" GOODBYE="CRUEL_WORLD" # end .set_env WORLD ... CRUEL_WORLD 下分配一组对象。

解决方案1: $array一系列对象,使其成为json_encode,然后将json转换为json

associative array

解决方案2:对数组进行迭代,并将每个$result=json_decode(json_encode($array),true); array_walk_recursive($result, function($value,$key){ print_r($value); print_r($key); }); 类型转换为object

array

答案 1 :(得分:2)

您始终可以为某些数据结构实现自定义递归迭代器。它可以是更灵活的解决方案。例如:

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);
}