如果只有一个结果,来自JSON的PHP数组会有所不同

时间:2016-12-14 04:34:38

标签: php arrays json

我正在使用澳大利亚邮政邮政编码loookup API,它返回一个JSON响应。

如果我搜索3094,则返回

Array
(
    [localities] => Array
        (
            [locality] => Array
                (
                    [category] => Delivery Area
                    [id] => 5588
                    [latitude] => -37.717549
                    [location] => MONTMORENCY
                    [longitude] => 145.121028
                    [postcode] => 3094
                    [state] => VIC
                )

        )

)

但是,如果我搜索一个与多个郊区(位置)相关联的邮政编码,例如3084,我会

Array
(
    [localities] => Array
        (
            [locality] => Array
                (
                    [0] => Array
                        (
                            [category] => Delivery Area
                            [id] => 5574
                            [latitude] => -37.739262
                            [location] => VIEWBANK
                            [longitude] => 145.096424
                            [postcode] => 3084
                            [state] => VIC
                        )

                    [1] => Array
                        (
                            [category] => Delivery Area
                            [id] => 5572
                            [latitude] => -37.756341
                            [location] => HEIDELBERG
                            [longitude] => 145.067145
                            [postcode] => 3084
                            [state] => VIC
                        )

                    [2] => Array
                        (
                            [category] => Delivery Area
                            [id] => 5573
                            [latitude] => -37.742893
                            [location] => ROSANNA
                            [longitude] => 145.065044
                            [postcode] => 3084
                            [state] => VIC
                        )

                    [3] => Array
                        (
                            [category] => Delivery Area
                            [id] => 5570
                            [latitude] => -37.7442195
                            [location] => BANYULE
                            [longitude] => 145.08793
                            [postcode] => 3084
                            [state] => VIC
                        )

                    [4] => Array
                        (
                            [category] => Delivery Area
                            [id] => 5571
                            [latitude] => -37.762519
                            [location] => EAGLEMONT
                            [longitude] => 145.068208
                            [postcode] => 3084
                            [state] => VIC
                        )

                )

        )

)

现在,通过

解码后
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_URL, $api_search_url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Auth-Key: ' . $authKey
));

$response = curl_exec($ch);
curl_close($ch);
$json_dec = json_decode($response,true);

我可以做像

这样的foreach循环
echo '<ul id="pcode_results" style="list-style:none;text-align:left;padding:10px;">';

foreach($json_dec['localities'] as $locality){
        echo '<li class="pcode_res" style="cursor:pointer;" id="' . $locality['postcode'] . ',' . $locality['location'] . ',' . $locality['state'] . '">' . $locality['postcode'] . ', ' . $locality['location'] . ', ' . $locality['state'] . '</li>';
    }

echo '</ul>';

当有一个结果如3094时似乎有用,但是当有多个结果如3084时则不行。

我最初将json解码作为一个对象数组,并且使用$ locality-&gt; postcode可以工作,但只有当结果超过1时才会有效。

我哪里错了?它需要能够处理阵列结构不同的两种结果场景(1个结果或多于1个)。

谢谢。

4 个答案:

答案 0 :(得分:1)

如果查看结果集,您会发现一些细节,当只有一个选项可用时,api只返回一个地点,当有多个选项可用时,它会返回一组地点。

当它仅返回一个结果第3级中的地点的详细信息时“,但在第4级返回多个地点信息时。

if (isset($json_dec['localities']['locality']['category'])) {
    // It means it returned a single result set, If details in Level 3 it is a single result
    $locality = $json_dec['localities']['locality'];
    echo '<li class="pcode_res" style="cursor:pointer;" id="' . $locality['postcode'] . ',' . $locality['location'] . ',' . $locality['state'] . '">' . $locality['postcode'] . ', ' . $locality['location'] . ', ' . $locality['state'] . '</li>';
} else {
    foreach ( $json_dec['localities']['locality'] as $locality ) {
        echo '<li class="pcode_res" style="cursor:pointer;" id="' . $locality['postcode'] . ',' . $locality['location'] . ',' . $locality['state'] . '">' . $locality['postcode'] . ', ' . $locality['location'] . ', ' . $locality['state'] . '</li>';
    }
}

答案 1 :(得分:0)

请尝试以下代码:

foreach($json_dec['locality'] as $locality)
{
   echo '<li class="pcode_res" style="cursor:pointer;" id="' . $locality['postcode'] . ',' . $locality['location'] . ',' . $locality['state'] . '">' . $locality['postcode'] . ', ' . $locality['location'] . ', ' . $locality['state'] . '</li>';
}

...谢谢

答案 2 :(得分:0)

您可以使用is_array检查它是否为数组:

foreach($json_dec['localities']['locality'] as $key => $value){
    if (is_array($value)) {
        foreach ($value as $locality) {
           echo '<li class="pcode_res" style="cursor:pointer;" id="' . $locality['postcode'] . ',' . $locality['location'] . ',' . $locality['state'] . '">' . $locality['postcode'] . ', ' . $locality['location'] . ', ' . $locality['state'] . '</li>';
        }
    } else {
        echo '<li class="pcode_res" style="cursor:pointer;" id="' . $value['postcode'] . ',' . $value['location'] . ',' . $value['state'] . '">' . $value['postcode'] . ', ' . $value['location'] . ', ' . $value['state'] . '</li>';
    }
}

答案 3 :(得分:0)

使用此代码访问所有数组元素:

foreach($json_dec['localities']['locality'] as $locality){

    echo '<li class="pcode_res" style="cursor:pointer;" id="' .$locality['postcode'] . ',' . $locality['location'] . ',' . $locality['state'] . '">' . $locality['postcode'] . ', ' . $locality['location'] . ', ' . $locality['state'] . '</li>';

}