如何在嵌套数组中找到值然后返回该父级? [关闭]

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在嵌套数组中找到值然后返回该父级? [关闭]相关的知识,希望对你有一定的参考价值。

我正在从Google Maps API返回一个嵌套数组。我想要做的是在数组中找到某种类型。我正在使用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
)

我需要做的是找到某种类型并返回long_nameshort_name。我希望这是有道理的。

所以基本上如果type = political然后返回整个数组,那么我可以访问long_name和short_name。

我尝试使用array_search,但我没有得到任何结果。

任何和所有的帮助表示赞赏!

答案

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

foreach($array['results'][0]['address_components'] as $v) 
    if(is_array($v['types']) && in_array('political', $v['types'])) 
        $result = $v;
        break;
    

如果你想要多个,那么使用$result[] = $v;并删除break

另一答案

你可以使用array_map函数来做到这一点

$arr = array(
  0 => array(
     'long_name'  => 'Bexar County',
     'short_name' => 'Bexar County',
     'types' => array(
        0 => 'administrative_area_level_2',
        1 => 'political'
     )
   )   
);

$findType = 'political';
$result = array_map(function($v) use ($findType)
      if(in_array($findType, $v['types'])) 
        return $v;
, $arr);

结果:

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

以上是关于如何在嵌套数组中找到值然后返回该父级? [关闭]的主要内容,如果未能解决你的问题,请参考以下文章

MongoDB:使用子代数组填充父代vs通过父代ID搜索子代

如何为特定页面创建父组件(以便能够在父级中添加反应上下文)?

javascript - 如果在子对象中找到值,则搜索嵌套对象数组并返回父对象[重复]

如何返回表单最高父级的名称

如何从嵌套的对象数组中获取每个父级的值

将 div 移动到其他父级,因此它继承自该父级 [重复]