【问题标题】:PHP: How to rename all keys of a Nested List (Array) to 'items'PHP:如何将嵌套列表(数组)的所有键重命名为“项目”
【发布时间】:2015-02-26 18:21:37
【问题描述】:

我需要“重新格式化”来自外部 API 的一些数据,以便它与 Sencha touch 的嵌套列表模块一起使用。我无法更改该外部 API 的数据输出。以下是我从 API 获得的数据示例:

$quest = array(
    'gastronomy' => [
        'restaurants' => [
            'italians' => [
                [
                    'title' => 'Al Castello',
                    'leaf' => true
                ],

                [
                    'title' => 'Italia',
                    'leaf' => true
                ]
            ],

            'asians' => [
                [
                    'title' => 'Gautam',
                    'leaf' => true
                ],

                [
                    'title' => 'Wok',
                    'leaf' => true
                ]
            ]
        ]
    ]
);

为了使其与 sencha touch 一起使用,在使用 PHP 服务“重新格式化”后数据必须如下所示:

$result = array(
    'items' => [
        [
            'title' => 'gastronomy',
            'items' => [
                [
                    'title' => 'restaurants',
                    'items' => [
                        [
                            'title' => 'italians',
                            'items' => [
                                [
                                    'title' => 'Al Castello',
                                    'leaf' => true
                                ],

                                [
                                    'title' => 'Italia',
                                    'leaf' => true
                                ]
                            ]
                        ],

                        [
                            'title' => 'asians',
                            'items' => [
                                [
                                    'title' => 'Gautam',
                                    'leaf' => true
                                ],

                                [
                                    'title' => 'Wok',
                                    'leaf' => true
                                ]
                            ]
                        ]
                    ]
                ]
            ]
        ]
    ]
);

我已经尝试了所有我能想到的方法,但没有成功。真正困扰我的是所有键都必须重命名为项目。 (因为当我使用递归函数时,我很难访问更深的嵌套项)

【问题讨论】:

    标签: php arrays algorithm multidimensional-array sencha-touch


    【解决方案1】:

    还没有测试过,但似乎一个相当简单的递归函数应该可以处理它。

    例如:

    function parseApi($arr) {
        $result = array();
        foreach ($arr as $key => $value) {
            if (isset($value['leaf'])) {
                $result[] = $value;
            } else {
                $result[] = array(
                    'title' => $key,
                    'items' => parseApi($value)
                );
            }
        }
    
        return $result;
    }
    
    $result = array( 'items' => $parseApi($quest);
    

    【讨论】:

    • 谢谢它的工作,现在我看到一切都变得如此清晰:-)
    【解决方案2】:

    您需要一个递归函数,它需要能够区分关联数组和数字索引数组。

    // from: http://stackoverflow.com/questions/173400/how-to-check-if-php-array-is-associative-or-sequential
    function isAssoc($arr) { return array_keys($arr) !== range(0, count($arr) - 1); }
    
    function itemize($foo) {
        $output = [];
        if( ! isAssoc($foo) ) {
            foreach( $foo as $value ) {
                if( is_array($value) ) {
                    $output[] = itemize($value);
                } else {
                    $output[] = $value;
                }
            }
        } else {
            foreach( $foo as $key => $value ) {
                if( is_array($value) ) {
                    $output[] = [
                        'title' => $key,
                        'items' => itemize($value)
                    ];
                } else {
                    $output[$key] = $value;
                }
            }
        }
        return $output;
    }
    
    echo json_encode(itemize($quest), JSON_PRETTY_PRINT);
    

    输出:

    [
        {
            "title": "gastronomy",
            "items": [
                {
                    "title": "restaurants",
                    "items": [
                        {
                            "title": "italians",
                            "items": [
                                {
                                    "title": "Al Castello",
                                    "leaf": true
                                },
                                {
                                    "title": "Italia",
                                    "leaf": true
                                }
                            ]
                        },
                        {
                            "title": "asians",
                            "items": [
                                {
                                    "title": "Gautam",
                                    "leaf": true
                                },
                                {
                                    "title": "Wok",
                                    "leaf": true
                                }
                            ]
                        }
                    ]
                }
            ]
        }
    ]
    

    【讨论】:

    • 也是解决这个问题的好方法。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2021-07-21
    • 2018-10-20
    • 2013-04-21
    • 1970-01-01
    • 2020-04-01
    • 1970-01-01
    • 2013-12-07
    相关资源
    最近更新 更多