使用PHP(Shiprocket API)从JSON对象中检索值

plupiseo  于 2023-02-01  发布在  PHP
关注(0)|答案(1)|浏览(121)

我正在通过Shiprocket API获取下面的json数据。现在我想从这个json中提取PHP代码中下面变量的值。
我尝试使用json_decode,但它不工作,并显示空值:

$data = json_decode($json);
$sr_status = $data['shipment_status'];

请建议代码检索下面的变量值.

装运状态、awb代码、快递公司标识

Array
(
    [0] => stdClass Object
        (
            [tracking_data] => stdClass Object
                (
                    [track_status] => 1
                    [shipment_status] => 7
                    [shipment_track] => Array
                        (
                            [0] => stdClass Object
                                (
                                    [id] => 180339484
                                    [awb_code] => 11150911492
                                    [courier_company_id] => 55
                                    [shipment_id] => 1711169662
                                    [order_id] => 233223781187
                                    [pickup_date] => 2023-01-11 03:02:00
                                    [delivered_date] => 2023-01-16 12:22:00
                                    [weight] => 0.25
                                    [packages] => 1
                                    [current_status] => Delivered
                                    [delivered_to] => Solapur
                                    [destination] => Solapur
                                    [consignee_name] => ABC
                                    [origin] => Ludhiana
                                    [courier_agent_details] =>
                                    [edd] =>
                                )

                        )

                    [shipment_track_activities] => Array
                        (
                            [0] => stdClass Object
                                (
                                    [date] => 2023-01-16 12:22:00
                                    [status] => 000-T-DL
                                    [activity] => SHIPMENT DELIVERED
                                    [location] => SOLAPUR
                                    [sr-status] => 7
                                    [sr-status-label] => DELIVERED
                                )

                            [1] => stdClass Object
                                (
                                    [date] => 2023-01-16 11:34:00
                                    [status] => 002-S-UD
                                    [activity] => SHIPMENT OUTSCAN
                                    [location] => SOLAPUR
                                    [sr-status] => 17
                                    [sr-status-label] => OUT FOR DELIVERY
                                )

                                                 

                        )

                    [track_url] => https://shiprocket.co//tracking/11150911492
                    [etd] => 2023-01-14 17:02:00
                    [qc_response] => stdClass Object
                        (
                            [qc_image] =>
                            [qc_failed_reason] =>
                        )

                )

        )

)
utugiqy6

utugiqy61#

你可以试试这个:

$array = ...; // Your array here
$data= json_decode($array);
$shipment_status = $data[0]->tracking_data->shipment_status;
$awb_code = $data[0]->tracking_data->shipment_track[0]->awb_code;
$courier_company_id = $data[0]->tracking_data->shipment_track[0]->courier_company_id;

或者使用$data = json_decode($json,true);返回一个数组,其中可以使用

foreach($data as $val) {
    $shipment_status = $val['tracking_data']['shipment_status'];
    foreach ($val['shipment_track'] as $value) {
        $awb_code = $value['awb_code'];
        $courier_company_id = $value['courier_company_id'];
    }
}

相关问题