php解析JSON混合的对象层次结构和数组来读取值

cigdeys3  于 2023-02-18  发布在  PHP
关注(0)|答案(2)|浏览(131)

需要从数组"places"读取值-〉然后从数组"parents"读取值
来自我使用REST API接收的响应($response):
然后用$json = json_decode($response, true);解码

Array
(
    [status_code] => 200
    [data] => Array
        (
            [places] => Array
                (
                    [0] => Array
                        (
                            [id] => 11878
                            [name] => Times Square
                            [name_suffix] => Manhattan, New York City, USA
                            [parents] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 32280039
                                            [name] => Theater District
                                            [level] => neighbourhood
                                        )

                                    [1] => Array
                                        (
                                            [id] => poi:28010161
                                            [name] => Manhattan Community Board 5
                                            [level] => region
                                        )

                                    [2] => Array
                                        (
                                            [id] => region:1981637
                                            [name] => Manhattan Island
                                            [level] => island
                                        )

                                    [3] => Array
                                        (
                                            [id] => poi:28043160
                                            [name] => Manhattan
                                            [level] => neighbourhood
                                        )
                                )

                            [perex] => Times Square (nicknamed “The Crossroads of the World”) is the best known square in New York City and also its beating heart.
                        )

                    [1] => Array
                        (
                            [id] => 11874
                            [name] => Empire State Building
                            [name_suffix] => Manhattan, New York City, USA
                            [parents] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => poi:32108808
                                            [name] => Midtown South
                                            [level] => neighbourhood
                                        )

                                    [1] => Array
                                        (
                                            [id] => poi:28010161
                                            [name] => Manhattan Community Board 5
                                            [level] => region
                                        )

                                    [2] => Array
                                        (
                                            [id] => region:1981637
                                            [name] => Manhattan Island
                                            [level] => island
                                        )

                                    [3] => Array
                                        (
                                            [id] => region:46071
                                            [name] => Manhattan
                                            [level] => neighbourhood
                                        )

                                )

                            [perex] => An iconic Art Deco skyscraper and one of the best known buildings in New York. It got its name from the city’s nickname the "Empire State".
                        )
                )

        )
)

我很难循环遍历对象,这些对象在请求时是未知的,并且对于每个单独的请求并不总是相同或可用的

foreach ($json->data as $places_list) {
    foreach ($places_list->places as $placesItem) {
        foreach($placesItem->(HELP HERE) as $place_detail) {
               echo json_encode($place_detail) . "\n";
        }
    }
}

您能否帮助实现一个通用函数,该函数允许(a)转到"位置"级别,然后(b)从"父级"检索值
谢谢你:)

8e2ybdfx

8e2ybdfx1#

你的代码与数据输出不匹配,因为它使用了对象表示法。但无论如何,这只是一个获得正确的数据层次结构的问题。
这段代码遍历places数组的列表,并从那里回送名称,然后一个内部循环遍历parents元素并从那里输出名称

foreach ($json['data']['places'] as $places_list) {
    echo $places_list['name']."-";
    foreach ($places_list['parents'] as $parents) {
        echo $parents['name'] ."/";
    }
    echo PHP_EOL;
}

其输出

Times Square-Theater District/Manhattan Community Board 5/Manhattan Island/Manhattan/
Empire State Building-Midtown South/Manhattan Community Board 5/Manhattan Island/Manhattan/
f4t66c6m

f4t66c6m2#

我们正在开发一个带有递归函数的通用解决方案。json是数组和对象的混合体,这使得即使是ICT专业人士也很难导入它。稍后通过DM与我联系,它应该在2023年3月之前准备好。

相关问题