we are developing a PHP project where we are parsing a JSON object which is multidimensional. We are unable to access a variable value using a key value pair method using foreach.
Response which need to be parsed:
{
"cash_pickups_count": 0.0,
"cod_count": 0,
"success": false,
"package_count": 1,
"upload_wbn": "UPL11355346020539699686",
"replacement_count": 0,
"cod_amount": 0.0,
"prepaid_count": 0,
"rmk": "An internal Error has occurred, Please get in touch with client.support@delhivery.com",
"pickups_count": 0,
"packages": [
{
"status": "Fail",
"waybill": "6009910000136",
"serviceable": false,
"refnum": "26399",
"client": "INKDAD",
"remarks": ["Duplicate order id"],
"sort_code": null,
"cod_amount": 0.0,
"payment": "Pre-paid"
}
],
"cash_pickups": 0.0
}
Code via which we are parsing:
$res = json_decode($result,true);
foreach ($res as $key => $val) {
echo $key;
if(($key) == "packages")
{
echo "entered in this loop";
foreach($val as $key1 => $value){
echo $key1;
if(($key1)== "waybill"){
$waybill = ;
echo $waybill;
}
}
}
}
We are getting an Array to string operation error.
Can you please let us know what is an issue over here?