【问题标题】:How do i make array of id in nested json如何在嵌套的 json 中制作 id 数组
【发布时间】:2019-09-30 07:33:27
【问题描述】:

如何将此 json 转换为仅包含 file_id 的数组

 {
        "file_id": 1,
        "name": "r1",
        "file_parent_id": null,
        "subfolder": [
            {
                "file_id": 3,
                "name": "r1.1",
                "file_parent_id": 1,
                "subfolder": [
                    {
                        "file_id": 7,
                        "name": "r1.1.1",
                        "file_parent_id": 3,
                        "subfolder": [
                            {
                                "file_id": 9,
                                "name": "r1.1.1.1",
                                "file_parent_id": 7
                            }
                        ]
                    }
                ]
            },
            {
                "file_id": 5,
                "name": "r1.2",
                "file_parent_id": 1
            }
        ]
    },

到这里

$arrayid=[1,3,7,9,5]

【问题讨论】:

    标签: php arrays json


    【解决方案1】:

    您可以将json_decodearray_walk_recursive 一起使用

    $arr = json_decode($json,true);
    $a   = [];
    array_walk_recursive($arr, function($v,$k) use(&$a){
     if($k == 'file_id') $a[] = $v;
    });
    

    演示:https://3v4l.org/5CcZt

    【讨论】:

    • 不错的解决方案小伙子
    猜你喜欢
    • 2015-11-08
    • 2022-11-30
    • 1970-01-01
    • 2020-10-07
    • 1970-01-01
    • 1970-01-01
    • 2021-11-04
    • 1970-01-01
    • 2018-06-24
    相关资源
    最近更新 更多