Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何在嵌套数组中找到一个值,然后返回该父级?_Php_Arrays_Api_Google Maps Api 3 - Fatal编程技术网

Php 如何在嵌套数组中找到一个值,然后返回该父级?

Php 如何在嵌套数组中找到一个值,然后返回该父级?,php,arrays,api,google-maps-api-3,Php,Arrays,Api,Google Maps Api 3,我正在从GoogleMapsAPI返回一个嵌套数组。我要做的是在数组中找到某种类型。我使用json_decode将结果从json转换为PHP。 下面是返回的响应: Array ( [results] => Array ( [0] => Array ( [address_components] => Array (

我正在从GoogleMapsAPI返回一个嵌套数组。我要做的是在数组中找到某种类型。我使用json_decode将结果从json转换为PHP。 下面是返回的响应:

Array
(
[results] => Array
    (
        [0] => Array
            (
                [address_components] => Array
                    (
                        [0] => Array
                            (
                                [long_name] => 78210
                                [short_name] => 78210
                                [types] => Array
                                    (
                                        [0] => postal_code
                                    )

                            )

                        [1] => Array
                            (
                                [long_name] => San Antonio
                                [short_name] => San Antonio
                                [types] => Array
                                    (
                                        [0] => locality
                                        [1] => political
                                    )

                            )

                        [2] => Array
                            (
                                [long_name] => Bexar County
                                [short_name] => Bexar County
                                [types] => Array
                                    (
                                        [0] => administrative_area_level_2
                                        [1] => political
                                    )

                            )

                        [3] => Array
                            (
                                [long_name] => Texas
                                [short_name] => TX
                                [types] => Array
                                    (
                                        [0] => administrative_area_level_1
                                        [1] => political
                                    )

                            )

                        [4] => Array
                            (
                                [long_name] => United States
                                [short_name] => US
                                [types] => Array
                                    (
                                        [0] => country
                                        [1] => political
                                    )

                            )

                    )

                [formatted_address] => San Antonio, TX 78210, USA
                [geometry] => Array
                    (
                        [bounds] => Array
                            (
                                [northeast] => Array
                                    (
                                        [lat] => 29.418426
                                        [lng] => -98.422923
                                    )

                                [southwest] => Array
                                    (
                                        [lat] => 29.374093
                                        [lng] => -98.499778
                                    )

                            )

                        [location] => Array
                            (
                                [lat] => 29.3979578
                                [lng] => -98.4677851
                            )

                        [location_type] => APPROXIMATE
                        [viewport] => Array
                            (
                                [northeast] => Array
                                    (
                                        [lat] => 29.418426
                                        [lng] => -98.422923
                                    )

                                [southwest] => Array
                                    (
                                        [lat] => 29.374093
                                        [lng] => -98.499778
                                    )

                            )

                    )

                [place_id] => ChIJy9U5Yzj2XIYRRhV9xWIghyU
                [types] => Array
                    (
                        [0] => postal_code
                    )

            )

    )

[status] => OK
)
我需要做的是找到一个特定的类型并返回那个长名称或短名称。我希望这是有道理的

基本上,如果type=political,那么返回整个数组,这样我就可以访问long\u名称和short\u名称

我尝试使用数组搜索,但没有得到任何结果


感谢您的任何帮助

这将返回找到的第一个数组:

foreach($array['results'][0]['address_components'] as $v) {
    if(is_array($v['types']) && in_array('political', $v['types'])) {
        $result = $v;
        break;
    }
}
如果需要多个,则使用$result[]=$v;然后删除中断。

您可以使用数组映射功能来执行此操作

结果:

Array
(
  [0] => Array
    (
        [long_name] => Bexar County
        [short_name] => Bexar County
        [types] => Array
            (
                [0] => administrative_area_level_2
                [1] => political
            )
    )
 )

看起来我得到了一个错误,参数2应该是一个数组。对于这一行,如果数组'political',$v['types']{那么你的数组不是你在问题中发布的。已编辑。我已经添加了来自Google API的完整响应。我是否缺少其他内容?你缺少['results'][0]['address\u components']明白了。非常感谢!我真的非常感谢您的帮助!嗨。我也从这段代码中得到了一个错误。行ifin_数组$findType,$v['types']是说in_数组应该是一个数组。但它是一个字符串,因为它是$findType。想法?@Lz430请分享你的完整数组。我在上面的问题代码中添加了完整的答案。你可以在这里看到答案:只需键入邮政编码78210
Array
(
  [0] => Array
    (
        [long_name] => Bexar County
        [short_name] => Bexar County
        [types] => Array
            (
                [0] => administrative_area_level_2
                [1] => political
            )
    )
 )