对 stdClass 对象使用 array_walk_recursive() [英] using array_walk_recursive() for stdClass objects

查看:48
本文介绍了对 stdClass 对象使用 array_walk_recursive()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经浏览了这里的一些答案,但似乎没有使用这种方法?

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

我有一个项目数组,这些项目是对象.对象可以有一个键,它是 'children' 和 'children' 是一个对象数组等.

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?

示例:

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

            )

推荐答案

$array 下分配对象数组.

Assign an array of objects under $array.

解决方案 1: json_encode 对象数组使其成为 json 然后将 json 转换为关联数组.

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

解决方案 2: 迭代数组并将每个 object 类型转换为 array.

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

这篇关于对 stdClass 对象使用 array_walk_recursive()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆