PHP objects (JSON)

How do I access this object??

I can access this with payer->name->given_name

"payer":{ 
    "name":{
        "given_name":"John",

But how do I get to this one??
When it is an object within an object??

"purchase_units":[{
    "payments":{
        "captures":[{
             "id":"3EP33639BR568753A",

You have a mix of arrays and objects, so you need to use indexes as well.

My php knowledge is very basic, but to illustrate

$json_string = <<<EOD
{
    "purchase_units": [
        {
            "payments": {
                "captures": [
                    {
                        "id":"3EP33639BR568753A"
                    }
                ]
            }
        }
    ]
}
EOD;

$json_ob = json_decode($json_string);

To access the the nested id

echo $json_ob->purchase_units[0]->payments->captures[0]->id;
// 3EP33639BR568753A
1 Like

Basically, Every opening piece of punctuation in a JSON translates as you step across its boundary.

[ becomes [index]
{ becomes →
and : is a name to be added to the chain.

"purchase_units":[{"payments":{"captures":[{"id":"3EP33639BR568753A"
brain parse:

Output: <null>. Target: "3EP33639BR568753A"

Read: "purchase_units":   , interpret: step
Output: purchase_units

Read: [    , interpret: enter array
Target ID in array: 0 (This is skipping a whole bunch of stuff re: finding the target/backwalking through an array)
Output: purchase_units[0]

Read: {    , interpret: enter object
Output: purchase_units[0]->

Read: "payments":    , interpret: step
Output: purchase_units[0]->payments

Read: {    , interpret: enter object
Output: purchase_units[0]->payments->

Read: "captures":    , interpret: step
Output: purchase_units[0]->payments->captures

Read: [    , interpret: enter array
Target ID in array: 0
Output: purchase_units[0]->payments->captures[0]

Read: {    , interpret: enter object
Output: purchase_units[0]->payments->captures[0]->

Read: "id":    , interpret: step
Output: purchase_units[0]->payments->captures[0]->id

Read: "3EP33639BR568753A",  interpret: Target found. Return output.