I am using the Australia Post postcode loookup API which returns a JSON response.
If I search for 3094, it returns
Array
(
[localities] => Array
(
[locality] => Array
(
[category] => Delivery Area
[id] => 5588
[latitude] => -37.717549
[location] => MONTMORENCY
[longitude] => 145.121028
[postcode] => 3094
[state] => VIC
)
)
)
But, if I search for a postcode that has more than one suburb (location) associated, such as 3084, I get
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
)
)
)
)
Now, after decoding via
$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);
I can do a foreach loop like
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>';
and it seems to work when there is one result such as 3094, but not when there are multiple results like 3084.
I originally has the json decode as an object array, and doing $locality->postcode would work, but only if there was more than 1 result.
Where am I going wrong? It needs to be able to cope with both scenarios of results (1 result or more than 1) where the array structure is different.
Thank you.
If you look at the resultset you'll find a little details, When only one option is available api is returning only one locality and when multiple options available it is returning set of localities.
When it is returning only one result details of locality in Level 3, but when returning multiple details of locality in Level 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>';
}
}
Please try this code :
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>';
}
Thanks...
You can use is_array to check if its an 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>';
}
}
Use this code to access all array elements:
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>';
}