0

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.

4 Answers 4

1

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>';
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

You sir (assuming you are a he) are a genius ! This method works perfectly. And your second sentence clearly explains what I was having trouble conveying.
0

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...

1 Comment

It will not satisfy the first condition. and secondly there is no 'locality' index in $json_dec. Its under 'localities', $json_dec['localities']['locality'];
0

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>';

}

1 Comment

t will not satisfy the first condition.
0

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>';
    }
}

3 Comments

It's an array in both scenarios, eh?
@adrianp Thanks. It was never coming back as an array. I tried is_array($json_dec_arr['localities']['locality']) and this comes back for both circumstances. Isn't 'locality' always an array, but just depends if it contains arrays or just the data? $json_dec_arr is just decoded as array so I know if it is decoded as object or array.
@Daerik and RossS you are right, next time I'll test my answer first! I'm going to edit my answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.