0

i have an json array

$response5 = $tracking->info('ups','1ZA74Y990304232598',array('title','order_id'));


Array
(
    [meta] => Array
        (
            [code] => 200
        )

    [data] => Array
        (
            [tracking] => Array
                (
                    [id] => 55a3a81a3398c2e12f2dc9af
                    [created_at] => 2015-07-13T11:59:22+00:00
                    [updated_at] => 2015-07-13T14:59:23+00:00
                    [last_updated_at] => 2015-07-13T14:59:23+00:00
                    [tracking_number] => 1ZA74Y990304232598
                    [slug] => ups
                    [active] => 1
                    [android] => Array
                        (
                        )

                    [custom_fields] => 
                    [customer_name] => 
                    [delivery_time] => 4
                    [destination_country_iso3] => USA
                    [emails] => Array
                        (
                        )

                    [expected_delivery] => 2015-07-16
                    [ios] => Array
                        (
                        )

                    [note] => 
                    [order_id] => 
                    [order_id_path] => 
                    [origin_country_iso3] => USA
                    [shipment_package_count] => 1
                    [shipment_pickup_date] => 2015-07-08T16:00:00
                    [shipment_delivery_date] => 
                    [shipment_type] => GROUND
                    [shipment_weight] => 4.6
                    [shipment_weight_unit] => lb
                    [signed_by] => 
                    [smses] => Array
                        (
                        )

                    [source] => api
                    [tag] => InTransit
                    [title] => 1ZA74Y990304232598
                    [tracked_count] => 2
                    [unique_token] => b16qZz90fg
                    [checkpoints] => Array
                        (
                            [0] => Array
                                (
                                    [slug] => ups
                                    [city] => 
                                    [created_at] => 2015-07-13T11:59:23+00:00
                                    [location] => US
                                    [country_name] => US
                                    [message] => BILLING INFORMATION RECEIVED
                                    [country_iso3] => USA
                                    [tag] => InfoReceived
                                    [checkpoint_time] => 2015-07-09T18:21:16
                                    [coordinates] => Array
                                        (
                                        )

                                    [state] => 
                                    [zip] => 
                                )

                            [1] => Array
                                (
                                    [slug] => ups
                                    [city] => OBETZ
                                    [created_at] => 2015-07-13T11:59:23+00:00
                                    [location] => OBETZ, OH, US
                                    [country_name] => OBETZ, OH, US
                                    [message] => ORIGIN SCAN
                                    [country_iso3] => USA
                                    [tag] => InTransit
                                    [checkpoint_time] => 2015-07-10T20:41:00
                                    [coordinates] => Array
                                        (
                                        )

                                    [state] => OH
                                    [zip] => 
                                )

                            [2] => Array
                                (
                                    [slug] => ups
                                    [city] => OBETZ
                                    [created_at] => 2015-07-13T11:59:23+00:00
                                    [location] => OBETZ, OH, US
                                    [country_name] => OBETZ, OH, US
                                    [message] => DEPARTURE SCAN
                                    [country_iso3] => USA
                                    [tag] => InTransit
                                    [checkpoint_time] => 2015-07-10T21:58:00
                                    [coordinates] => Array
                                        (
                                        )

                                    [state] => OH
                                    [zip] => 
                                )

                            [3] => Array
                                (
                                    [slug] => ups
                                    [city] => INDIANAPOLIS
                                    [created_at] => 2015-07-13T11:59:23+00:00
                                    [location] => INDIANAPOLIS, IN, US
                                    [country_name] => INDIANAPOLIS, IN, US
                                    [message] => ARRIVAL SCAN
                                    [country_iso3] => USA
                                    [tag] => InTransit
                                    [checkpoint_time] => 2015-07-11T01:36:00
                                    [coordinates] => Array
                                        (
                                        )

                                    [state] => IN
                                    [zip] => 
                                )

                            [4] => Array
                                (
                                    [slug] => ups
                                    [city] => INDIANAPOLIS
                                    [created_at] => 2015-07-13T11:59:23+00:00
                                    [location] => INDIANAPOLIS, IN, US
                                    [country_name] => INDIANAPOLIS, IN, US
                                    [message] => DEPARTURE SCAN
                                    [country_iso3] => USA
                                    [tag] => InTransit
                                    [checkpoint_time] => 2015-07-11T04:25:00
                                    [coordinates] => Array
                                        (
                                        )

                                    [state] => IN
                                    [zip] => 
                                )

                            [5] => Array
                                (
                                    [slug] => ups
                                    [city] => HODGKINS
                                    [created_at] => 2015-07-13T11:59:23+00:00
                                    [location] => HODGKINS, IL, US
                                    [country_name] => HODGKINS, IL, US
                                    [message] => ARRIVAL SCAN
                                    [country_iso3] => USA
                                    [tag] => InTransit
                                    [checkpoint_time] => 2015-07-11T07:23:00
                                    [coordinates] => Array
                                        (
                                        )

                                    [state] => IL
                                    [zip] => 
                                )

                            [6] => Array
                                (
                                    [slug] => ups
                                    [city] => HODGKINS
                                    [created_at] => 2015-07-13T14:59:23+00:00
                                    [location] => HODGKINS, IL, US
                                    [country_name] => HODGKINS, IL, US
                                    [message] => DEPARTURE SCAN
                                    [country_iso3] => USA
                                    [tag] => InTransit
                                    [checkpoint_time] => 2015-07-13T09:25:00
                                    [coordinates] => Array
                                        (
                                        )

                                    [state] => IL
                                    [zip] => 
                                )

                        )

                    [tracking_account_number] => 
                    [tracking_destination_country] => 
                    [tracking_key] => 
                    [tracking_postal_code] => 
                    [tracking_ship_date] => 
                )

        )

)

I am using aftership api here to display the tracking details of a courier .

I need to loop the checkpoint array here and display all the tracking stages.

How to loop it .

I tried something like the below .

  <table style="border: 1px solid #CC9933;margin-top:50px;">
    <tr>
        <th class="porders">Date</th>
        <th class="porders">Location</th>
        <th class="porders">Status</th>
        <tr>
     <?php


     for($i=0; $i<count($response5['data']['tracking']['checkpoints']); $i++) {

        echo "<td class=porders>$response5['data']['tracking']['checkpoints'][$i]['created_at']."</td>";
        echo "<td class=porders>$response5['data']['tracking']['checkpoints'][$i]['country_name']."</td>";
        echo "<td class=porders>$response5['data']['tracking']['checkpoints'][$i]['message']."</td>";
    }
     ?>   


    </tr>
  </table>
3
  • 1
    you don't iterate a "json array". you take json string, decode it to a native data structure, then iterate THAT. and once it's decoded to native, it's not different than any OTHER array/object/whatever. Commented Jul 13, 2015 at 16:19
  • What's wrong with your code? You could also use foreach instead your for block. Commented Jul 13, 2015 at 16:22
  • its not getting iterated Commented Jul 13, 2015 at 16:27

3 Answers 3

1

use json_decode to move the data to a PHP array.

<?php
$json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';

$my_array = json_decode($json);
foreach ($my_array as $element) {
    print $element . " , ";
}
var_dump($my_array);
?>
Sign up to request clarification or add additional context in comments.

1 Comment

The OP's data is already in a PHP array, he's asking how to iterate through it.
1

First of all your $obj is already a decoded array, so there is no need to use the $response5 variable (unless you really want to use it as text).

As for iterating it would look like this:

foreach ($obj['data']['tracking']['checkpoints'] as $key => $checkpoint) {
    foreach($checkpoint as $nodeName => $value) {
        echo '<td class="orders">'.$nodeName.' => '.$value.'</td>;
    }
}

In order to list your coordinates you would need to run foreach on that node again

Comments

0
   <?php

    foreach ($response5['data']['tracking']['checkpoints'] as $key => $checkpoint) {//as key and value


        echo '<td class="porders">'.$checkpoint['created_at'].'</td>';
        echo '<td class="porders">'.$checkpoint['country_name'].'</td>';
        echo '<td class="porders">'.$checkpoint['message'].'</td>';

}

     ?>   

Comments

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.