I am trying to access specific value from a JSON string. Perhaps its due to my lack of understanding of JSON structure and how to access. Please help. I am trying to access value of "MerchantRequestID" and "PhoneNumber"
I have tried following to get MerchantRequestID and getting error. Also, without a loop, can we directly access value of particular element if structure is known?
$JSON_String = '{"Body":{"stkCallback":{"MerchantRequestID":"23226-16663390-1","CheckoutRequestID":"28062020192185","ResultCode":0,"ResultDesc":"It worked.","CallbackMetadata":{"Item":[{"Name":"Amount","Value":5.00},{"Name":"ReceiptNumber","Value":"XX223344"},{"Name":"Balance"},{"Name":"TransactionDate","Value":20200628142747},{"Name":"PhoneNumber","Value":12345678}]}}}}';
echo $JSON_String;
$jsonInput = json_decode($JSON_String, true);
foreach($jsonInput['Body'] as $body){
foreach($body['stkCallback'] as $callBack){
echo $callBack['MerchantRequestID'];
}
}
var_dump($jsonInput)to know the path that leads to the required property