micky007 Posted August 20, 2022 Share Posted August 20, 2022 (edited) Hi, I've been reading a lot of tutorials from other sites on how to get a value from a JSON array but none of them seem to work for me. I have the below JSON I'm trying to get the value of OrderNumber and place it within its own variable. But all the tutorials I've read don't have the [ at the beginning of their JSON. So everything I'm trying doesn't seem to be working for me. [{"OrgID":"1234567890987654321","OrderNumber":"M123456","OrigOrderNumber":"","Placed":"2022-08-17T14:04:47.653","PostCode":"AB1 2CD","BackOfficeOrderNumber":"12345678","CustomerName":"My Name","OutletName":"My Name Two","OrderStatus":"Delivered","OrderRef":"O-0012345678","AddressCount":1}] Any help would be much appreciated please. Thank you. Edited August 20, 2022 by micky007 Quote Link to comment Share on other sites More sharing options...
Solution Barand Posted August 20, 2022 Solution Share Posted August 20, 2022 <?php $jsn = '[{"OrgID":"1234567890987654321","OrderNumber":"M123456","OrigOrderNumber":"","Placed":"2022-08-17T14:04:47.653","PostCode":"AB1 2CD","BackOfficeOrderNumber":"12345678","CustomerName":"My Name","OutletName":"My Name Two","OrderStatus":"Delivered","OrderRef":"O-0012345678","AddressCount":1}]'; //convert to an array $arr = json_decode($jsn, 1); // output the array to view structure echo '<pre>' . print_r($arr, 1) . '</pre>'; ?> The resulting array is and you then access you required value by following the array keys (indicated), so you want $order_number = $arr[0]['OrderNumber']; 1 Quote Link to comment Share on other sites More sharing options...
micky007 Posted August 20, 2022 Author Share Posted August 20, 2022 Thank you very much @Barand. Worked exactly as i wanted. I do have another one I'm not struggling with which is this, I'm trying to get the ConsignmentNo. I have tried each of the below without success. echo $arr->OrderAddresses->ConsignmentNo; echo $arr->OrderAddresses[0]->ConsignmentNo; echo $arr[0]['ConsignmentNo']; echo $arr[1]['ConsignmentNo']; Here is the Array. Array ( [OrderLines] => Array ( [0] => Array ( [Description] => Product Name [Brand] => ABCD [Quantity] => 1 [SellingPrice] => 3.68 [BudgetCode] => [LedgerCode] => [OnHold] => [PartNum] => FG5675446345 ) ) [OrderAddresses] => Array ( [0] => Array ( [BOOrderNum] => 6547467 [AccountName] => Name 1 [AccountNumber] => [FirstName] => Fname [LastName] => Lname [Address1] => My Address [Address2] => 2nd PArt [City] => MANCHESTER [County] => [PostCode] => AB1 2CD [Country] => UK [ConsignmentNo] => 547645363546 [NumberOfContainers] => 1 [PackageType] => Box [Carrier] => FEDEX ) ) [OrderNumber] => M123456 [Placed] => 2022-08-17T14:04:47.653 [TotalPrice] => 3.68 [ConsignmentNo] => [DeliveryType] => Standard [Weight] => 0.45 ) Thank you again for your support. Quote Link to comment Share on other sites More sharing options...
Barand Posted August 20, 2022 Share Posted August 20, 2022 Just as before echo $arr['OrderAddresses'][0]['ConsignmentNo']; //--> 547645363546 "- >" is for object properties not array keys 1 Quote Link to comment Share on other sites More sharing options...
micky007 Posted August 20, 2022 Author Share Posted August 20, 2022 Ah ok I understand now. Thank you very much! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.