Home > database >  Retrieve a value from JSON Object using PHP (Shiprocket API)
Retrieve a value from JSON Object using PHP (Shiprocket API)

Time:02-02

I am getting below json data thru Shiprocket API. Now I want to extract value of below variables in PHP code from this json.

I have tried to use json_decode but it did not work and show null value:

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

Please suggest the code to retrieve below variables value.

shipment_status , awb_code , courier_company_id

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

                )

        )

)


CodePudding user response:

you can try this:

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

Or use $data = json_decode($json,true); which return an array where you can use

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'];
    }
}
  • Related