如何迭代json数组


how to iterate json array

我有一个json数组

$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] => 
                )
        )
)

我在这里使用aftership api来显示快递的跟踪细节。

我需要在这里循环检查点数组并显示所有跟踪阶段。

如何循环。

我尝试了下面的方法。

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

使用json_decode将数据移动到PHP数组。

<?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);
?>

首先,您的$obj已经是一个解码的数组,因此没有必要使用$response5变量(除非您真的想将其用作文本)。

对于迭代,它看起来像这样:

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

为了列出您的坐标,您需要在该节点上再次运行foreach

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