多维数组值中的JSON对象值(Google GEO数据)

时间:2018-07-31 14:58:31

标签: php arrays json geolocation

我想要的是类型为postal_code的列表的long_name值。每个地址变体都在不同的阵列ID上,否则我会像这样使用semething:

$jsonobj->results[0]->address_components[2]->long_name;

那是行不通的;(

这是来自Google GEO位置API的代码

{
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "2565",
               "short_name" : "2565",
               "types" : [ "postal_code", "postal_code_prefix" ]
            },
            {
               "long_name" : "Den Haag",
               "short_name" : "Den Haag",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Den Haag",
               "short_name" : "Den Haag",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "Zuid-Holland",
               "short_name" : "ZH",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "Nederland",
               "short_name" : "NL",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "2565 Den Haag, Nederland",
         "geometry" : {
            "bounds" : {
...

因此,简而言之,我如何获得“类型==邮政编码”的“ long_name”值(也许使用in_array(postal_code,$ types)?)。

答案已经给出,但也想在此处发布我的函数版本:

$city          = getAddressDetail($jsonobj, 'locality');
$zipcode       = getAddressDetail($jsonobj, 'postal_code');

function getAddressDetail($jsonobj, $attr){
    // Loop over each result
    foreach ($jsonobj->results as $result) {
        // Loop over each address_component of a given result
        foreach ($result->address_components as $component) {
            // Check for 'postal_code' in the types array
            if (in_array($attr, $component->types, true)) {
                // Extract the 'long_name'
                return $component->long_name;
            }
        }
    }
}

1 个答案:

答案 0 :(得分:1)

您只需要嵌套几个foreach循环,然后使用in_array()

<?php

// Loop over each result
foreach ($jsonobj->results as $result) {
    // Loop over each address_component of a given result
    foreach ($result->address_components as $component) {
        // Check for 'postal_code' in the types array
        if (in_array('postal_code', $component->types, true)) {
            // Extract the 'long_name'
            echo $component->long_name . "\n";
        }
    }
}

如果要使搜索更通用,可以将其提取为功能:

<?php

function find_by_type($obj, $type) {
    $res = [];

    foreach ($obj->results as $result) {
        foreach ($result->address_components as $component) {
            if (in_array($type, $component->types, true)) {
                $res[] = $component->long_name;
            }
        }
    }

    return $res;
}

$res = find_by_type($jsonobj, 'postal_code');

var_dump($res);

如果您真的想看中,请传递callable,它将提取您想要的字段:

function find_by_type($obj, $type, $extractor) {
    $res = [];

    foreach ($obj->results as $result) {
        foreach ($result->address_components as $component) {
            if (in_array($type, $component->types, true)) {
                $res[] = $extractor($component);
            }
        }
    }

    return $res;
}

$res = find_by_type($jsonobj, 'postal_code', function($c) {
    return $c->short_name;
});

var_dump($res);