【问题标题】:using array_walk_recursive() for stdClass objects对 stdClass 对象使用 array_walk_recursive()
【发布时间】:2017-04-18 14:16:28
【问题描述】:

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

我有一个项目数组,这些项目是对象。该对象可以有一个键是“孩子”,而“孩子”是一个对象数组等。

有没有办法做到这一点?

示例:

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

            )

【问题讨论】:

    标签: php multidimensional-array array-walk


    【解决方案1】:

    $array 下分配一组对象。

    解决方案 1: json_encode 对象数组使其成为 json,然后将 json 转换为 associative array

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

    解决方案 2: 遍历数组并将每个 object 类型转换为 array

    array_walk($array,function(&$value,$key){
        $value=(array) $value;
    });
    array_walk_recursive($array, function($value,$key){
        print_r($value);
        print_r($key);
    });
    

    【讨论】:

    • 谢谢。我想不可能在将它们保留为对象的同时循环遍历?
    • @RyanHipkiss array_walk_recursive 仅适用于数组而非对象。
    • 谢谢,这个解决方案确实有效。我希望这是因为 'children' 属性是一个可以工作的数组。不过不用担心。
    • 欢迎@RyanHipkiss 感谢您的接受...如果您有一些包含嵌套对象的嵌套数组,那么请使用第一个解决方案.. 它也适用于此..
    • 嗨 @SahilGulati 解决方案 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);
    }
    

    【讨论】:

      猜你喜欢
      • 2013-06-10
      • 2015-12-13
      • 2011-08-31
      • 1970-01-01
      • 2013-06-23
      • 2021-01-19
      • 2015-06-12
      • 2012-01-19
      • 2010-12-06
      相关资源
      最近更新 更多