PHP:如何将嵌套列表(数组)的所有键重命名为';项目';


PHP: How to rename all keys of a Nested List (Array) to 'items'

我需要"重新格式化"一些来自外部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
                                ]
                            ]
                        ]
                    ]
                ]
            ]
        ]
    ]
);

我想尽一切办法,但都没有成功。真正困扰我的是,所有密钥都必须重命名为项。(当我使用递归函数时,我很难访问更深层次的嵌套项)

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

例如:

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

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

// 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
                            }
                        ]
                    }
                ]
            }
        ]
    }
]