简体   繁体   中英

Get specific value from JSON Data

I want to get specific data values from json. Here is my json code

{"status":{"shipmentnumber":"5014402092","statusrow":[{"statusdate":"Mar 01st, 2021","statustime":"04:51 PM","statusmessage":"Delivered to "},{"statusdate":"Mar 01st, 2021","statustime":"07:08 AM","statusmessage":"Shipment has reached blueEX Gujranwala - Gujranwala"},{"statusdate":"Feb 26th, 2021","statustime":"10:43 PM","statusmessage":"Shipment is on route to Gujranwala"},{"statusdate":"Feb 26th, 2021","statustime":"10:42 PM","statusmessage":"Shipment reached blueEX Karachi Warehouse, Karachi"},{"statusdate":"Feb 26th, 2021","statustime":"03:36 PM","statusmessage":"Order information received, pending at Shipper's end."}]}}

I want to get "statusmessage": "Delivered to " data. Here is my code

$data['status']['statusrow'][0]['statusmessage']

But its returns { only.

Someone please guide what I did wrong

How are you converting the JSON string into a PHP data structure?

This works for me:

$json = '{"status":{"shipmentnumber":"5014402092","statusrow":[{"statusdate":"Mar 01st, 2021","statustime":"04:51 PM","statusmessage":"Delivered to "},{"statusdate":"Mar 01st, 2021","statustime":"07:08 AM","statusmessage":"Shipment has reached blueEX Gujranwala - Gujranwala"},{"statusdate":"Feb 26th, 2021","statustime":"10:43 PM","statusmessage":"Shipment is on route to Gujranwala"},{"statusdate":"Feb 26th, 2021","statustime":"10:42 PM","statusmessage":"Shipment reached blueEX Karachi Warehouse, Karachi"},{"statusdate":"Feb 26th, 2021","statustime":"03:36 PM","statusmessage":"Order information received, pending at Shipper\'s end."}]}}';
$data = json_decode( $json );

echo $data->status->statusrow[0]->statusmessage; // "Delivered to"
$data = '{"status":{"shipmentnumber":"5014402092","statusrow":[{"statusdate":"Mar 01st, 2021","statustime":"04:51 PM","statusmessage":"Delivered to "},{"statusdate":"Mar 01st, 2021","statustime":"07:08 AM","statusmessage":"Shipment has reached blueEX Gujranwala - Gujranwala"},{"statusdate":"Feb 26th, 2021","statustime":"10:43 PM","statusmessage":"Shipment is on route to Gujranwala"},{"statusdate":"Feb 26th, 2021","statustime":"10:42 PM","statusmessage":"Shipment reached blueEX Karachi Warehouse, Karachi"},{"statusdate":"Feb 26th, 2021","statustime":"03:36 PM","statusmessage":"Order information received, pending at Shippers end."}]}}';


$data = json_decode($data,true);
// print_r($data)
echo $data['status']['statusrow'][0]['statusmessage'];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM