I have this json:
{"id":"***hidden***","event_version":"1.0","create_time":"2020-07-28T04:09:33.415Z","resource_type":"checkout-order","resource_version":"2.0","event_type":"CHECKOUT.ORDER.APPROVED","summary":"An order has been approved by buyer","resource":{"update_time":"2020-07-28T04:09:05Z","create_time":"2020-07-28T04:08:53Z","purchase_units":[{"reference_id":"default","amount":{"currency_code":"CAD","value":"50.00"},"payee":{"email_address":"***hidden**","merchant_id":"***hidden***"},"custom_id":"THISVALUEIWANT","shipping":{"name":{"full_name":"John Doe"},"address":{"address_line_1":"1 Maire-Victorin","admin_area_2":"Toronto","admin_area_1":"ON","postal_code":"M5A 1E1","country_code":"CA"}},"payments":{"captures":[{"id":"***hidden***","status":"COMPLETED","amount":{"currency_code":"CAD","value":"50.00"},"final_capture":true,"seller_protection":{"status":"ELIGIBLE","dispute_categories":["ITEM_NOT_RECEIVED","UNAUTHORIZED_TRANSACTION"]},"seller_receivable_breakdown":{"gross_amount":{"currency_code":"CAD","value":"50.00"},"paypal_fee":{"currency_code":"CAD","value":"1.75"},"net_amount":{"currency_code":"CAD","value":"48.25"}},"links":[{"href":"https://api.sandbox.paypal.com/v2/payments/captures/something","rel":"self","method":"GET"},{"href":"https://api.sandbox.paypal.com/v2/payments/captures/something/refund","rel":"refund","method":"POST"},{"href":"https://api.sandbox.paypal.com/v2/checkout/orders/something","rel":"up","method":"GET"}],"create_time":"2020-07-28T04:09:05Z","update_time":"2020-07-28T04:09:05Z"}]}}],"links":[{"href":"https://api.sandbox.paypal.com/v2/checkout/orders/something","rel":"self","method":"GET"}],"id":"something","intent":"CAPTURE","payer":{"name":{"given_name":"John","surname":"Doe"},"email_address":"sb-***hidden","payer_id":"something","address":{"country_code":"CA"}},"status":"COMPLETED"},"links":[{"href":"https://api.sandbox.paypal.com/v1/notifications/webhooks-events/something","rel":"self","method":"GET"},{"href":"https://api.sandbox.paypal.com/v1/notifications/webhooks-events/something/resend","rel":"resend","method":"POST"}]}';
If i breakdown the structure, it goes like this:
{
"id":"***hidden***",
"event_version":"1.0",
"create_time":"2020-07-28T04:09:33.415Z",
"resource_type":"checkout-order",
"resource_version":"2.0",
"event_type":"CHECKOUT.ORDER.APPROVED",
"summary":"An order has been approved by buyer",
"resource":{
"update_time":"2020-07-28T04:09:05Z",
"create_time":"2020-07-28T04:08:53Z",
"purchase_units":[{
"reference_id":"default",
"amount":{
"currency_code":"CAD",
"value":"50.00"
},
"payee":{
"email_address":"***hidden***",
"merchant_id":"***hidden***"},
"custom_id":"THISISTHEVALUEIWANT",
...
I'd like to extract and store the value of custom_id in a php variable. Problem is I struggle in finding the way to extract this specific item which is, to my understanding, an object, within an array, within an object...
I've tried multiple combinations, I tried foreach loops, nothing worked so far.
The only thing that actually kind of worked is this:
$json = json_decode($jsonobj, true); // json is stored in $jsonobj variable
foreach($json as $elem) {
foreach ($elem['purchase_units'] as $item ) {
echo $item['custom_id'];
}
}
Which lets me get the value I want, but at the same time it throws me a whole bunch of E_WARNING type 2 error:
Invalid argument supplied for foreach()...
Illegal string offset 'purchase_units'...
Can you tell my how I could reach this specific value without flooding my php error log?
Thank you very much!
$json['item_B']['array_A'][0]['item_1']. But if I try$json['item_B']['array_A'][0]['item_2']['item_3'], I get an undefined index error.$json['item_B']['array_A'][0]['item_2']['item_3']that you've mention too. Which should be the most fragile but the easiest way.$jsonobj = '{"item_A": "Value A", "item_B": {"item_C": "Value C", "item_D": "Value D", "array_A": [{ "item_1": "Value 1", "item_2": { "item_3": "Value 3" } }] } }'; $json = json_decode($jsonobj, true); var_dump($json['item_B']['array_A'][0]['item_2']['item_3']);