Home > Software design >  JSON Data and PHP Handling
JSON Data and PHP Handling

Time:03-03

"response":{
    "RemanHeaderResponse":{
    "dsGetRemanHeaderResponse":{
    "dtRemanHeaderResponse":[{
    "OrderID":644,"BalancingUOM":"",
    "TransactionDescription":"TEST",
    "ExpectedDate":null,"TransactionJob":"",
    "TransactionReference":"",
    "RemanType":"METAL",
    "StartDate":null,"SupplierID":"",
    "SupplierShipFromSequence":0
}]
}
},"RemanInputResponse":{
    "dsGetRemanInputResponse":{
    "dtRemanInputResponse":[{
    "OrderID":644,"Sequence":1,"Key":"1",
    "LinkID":"",
    "ItemCode":"SHF300052",
    "ItemSize":"48\"X10'",
    "ItemDescription":"",
    "OrderQty":10.0,"OrderQtyUOM":"LF",
    "AffectUsage":true
},{
    "OrderID":644,"Sequence":2,"Key":"1",
    "LinkID":"",
    "ItemCode":"SHF300052",
    "ItemSize":"48\"X10'",
    "ItemDescription":"",
    "OrderQty":23.0,"OrderQtyUOM":"LF",
    "AffectUsage":true
},{
    "OrderID":644,"Sequence":3,"Key":"4",
    "LinkID":"",
    "ItemCode":"SHF300005",
    "ItemSize":"48\"X10'",
    "ItemDescription":"",
    "OrderQty":121.0,"OrderQtyUOM":"LF",
    "AffectUsage":true
},{
    "OrderID":644,"Sequence":4,"Key":"5",
    "LinkID":"",
    "ItemCode":"SHF300004",
    "ItemSize":"48\"X10'",
    "ItemDescription":"",
    "OrderQty":10.0,"OrderQtyUOM":"LF",
    "AffectUsage":true
}]
}
},"RemanOperationResponse":{
    "dsGetRemanOperationResponse":{
    
}
},"RemanOutputResponse":{
    "dsGetRemanOutputResponse":{
    
}
},"ReturnCode":0,"MessageText":""
}
}

I need to echo the ItemSize. How do I do so with this being nested ?

I am not sure how to do this :

$arr["response"]["RemanHeaderResponse"]["RemanHeaderResponse"]["dtRemanHeaderResponse"][0]["OrderID"]

Above syntax for instance will not even give me the order id.

CodePudding user response:

JSON is essentially just a string in PHP - you'll need to decode it with json_decode($arr) to handle it in PHP.

json_decode($arr) will create it as as object, or json_decode($arr, true) will create an associative array.

CodePudding user response:

<?php

$json = '{"response":{
    "RemanHeaderResponse":{
    "dsGetRemanHeaderResponse":{
    "dtRemanHeaderResponse":[{
    "OrderID":644,"BalancingUOM":"",
    "TransactionDescription":"TEST",
    "ExpectedDate":null,"TransactionJob":"",
    "TransactionReference":"",
    "RemanType":"METAL",
    "StartDate":null,"SupplierID":"",
    "SupplierShipFromSequence":0
}]
}
},"RemanInputResponse":{
    "dsGetRemanInputResponse":{
    "dtRemanInputResponse":[{
    "OrderID":644,"Sequence":1,"Key":"1",
    "LinkID":"",
    "ItemCode":"SHF300052",
    "ItemSize":"48\"X10\'",
    "ItemDescription":"",
    "OrderQty":10.0,"OrderQtyUOM":"LF",
    "AffectUsage":true
},{
    "OrderID":644,"Sequence":2,"Key":"1",
    "LinkID":"",
    "ItemCode":"SHF300052",
    "ItemSize":"48\"X10\'",
    "ItemDescription":"",
    "OrderQty":23.0,"OrderQtyUOM":"LF",
    "AffectUsage":true
},{
    "OrderID":644,"Sequence":3,"Key":"4",
    "LinkID":"",
    "ItemCode":"SHF300005",
    "ItemSize":"48\"X10\'",
    "ItemDescription":"",
    "OrderQty":121.0,"OrderQtyUOM":"LF",
    "AffectUsage":true
},{
    "OrderID":644,"Sequence":4,"Key":"5",
    "LinkID":"",
    "ItemCode":"SHF300004",
    "ItemSize":"48\"X10\'",
    "ItemDescription":"",
    "OrderQty":10.0,"OrderQtyUOM":"LF",
    "AffectUsage":true
}]
}
},"RemanOperationResponse":{
    "dsGetRemanOperationResponse":{
    
}
},"RemanOutputResponse":{
    "dsGetRemanOutputResponse":{
    
}
},"ReturnCode":0,"MessageText":""
}
}';

$arr = json_decode($json, true);
print_r($arr['response']['RemanHeaderResponse']['dsGetRemanHeaderResponse']['dtRemanHeaderResponse']['0']['OrderID']);

So, like i said in the comments:

You are looking at JSON. PHP cant read it like that, so you need to decode it first. Try to decode it first

Then access it.

Result of above print 644

Working example: https://www.tehplayground.com/zwIqVEAS5bC7FLhU

Edit: I did fix your JSON string, since it was littered with single quotes and double quotes alike by adding backslashes in front of your single quotes. For you, your json is probably already in a variable, but for me it wasnt, so it broke the string.

  • Related