Deprecated post. Issue resolved outside of this thread.
-
3You are looking at `JSON`. PHP cant read it like that, so you need to decode it first. Try to decode it first `$arr = json_decode($arr, TRUE)` This way you put it in an associative array named `$arr`. Now you can call it as an array item. If you use false or leave it blank, it will become an `object`. https://www.php.net/manual/en/function.json-decode.php for more information – Dorvalla Mar 03 '22 at 13:07
-
Does this answer your question? [How to extract and access data from JSON with PHP?](https://stackoverflow.com/questions/29308898/how-to-extract-and-access-data-from-json-with-php) – Dorvalla Mar 03 '22 at 13:12
2 Answers
<?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.
- 4,622
- 4
- 25
- 42
-
@EricVasilisin first `print_r` your result of your json_decode... i am gonna asume you didnt post your full json file, thats all i can think off right now. see if you are missing a tag. Also, your three comments in a row wont make me respond faster.. lol. And still wont work is not helpfull. Your echo is part of the error, but mostly it states above it why it fails, like cant find something in array. – Dorvalla Mar 03 '22 at 15:11
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.
EDIT:
You need to check your JSON data - for one you're missing a { at the start.
your item size is also messed up - "ItemSize":"48\"X10'", ?
see if you can copy and paste your JSON into an online decoder like this - if that doesn't work then you need to fix your data
once you are sure you're data's good then it's just a case of pointing to the correct place after the json_decode, here's an copy & paste working example with fixed data - hopefully you can figure it out from here:
<?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":""
}
}';
$decoded_json = json_decode($json, true);
print_r( $decoded_json['response']['RemanHeaderResponse']['dsGetRemanHeaderResponse']['dtRemanHeaderResponse'][0]['OrderID'] );
- 340
- 1
- 4
-
@EricVasilisin check your JSON data - i'm assuming it's just broken while copy/pasting but it's not valid JSON - see my edited answer. – Rhyan-WoodsAndWalker Mar 04 '22 at 11:20