Jump to content

Get Value of JSON Array


micky007
Go to solution Solved by Barand,

Recommended Posts

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 by micky007
Link to comment
Share on other sites

  • Solution
<?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

image.png.1215270857318ce5712df78d5ff1924a.png

and you then access you required value by following the array keys (indicated), so you want

$order_number = $arr[0]['OrderNumber'];

 

  • Great Answer 1
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.