rockstardev
rockstardev

Reputation: 13527

How to correctly reference multi-dimensional arrays?

I have the following array called "$order" (as printed out by "print_r"):

stdClass Object
(
    [products] => Array
    (
        [0] => stdClass Object
        (
             [data] => Array
             (
                 [attributes] => Array
                 (
                     [ID] => Array
                     (
                         [0] => 57
                     )
                  )
             )
         )
    )
)

My question is, how do I reference "57"? I thought it would be something like this:

$order->products[0]->data[attributes][ID][0];

But this doesn't work. What am I missing?

Upvotes: 0

Views: 979

Answers (3)

Philippe Gerber
Philippe Gerber

Reputation: 17836

You're missing some quotes for the array keys. Otherwise it makes PHP think that attributes or ID is a constant (define('ID', 'foobar'); echo ID;).

$order->products[0]->data['attributes']['ID'][0];

Upvotes: 2

rockstardev
rockstardev

Reputation: 13527

Nevermind. It's like this: $order->products[0]->data["attributes"]["ID"][0];

Upvotes: 0

cletus
cletus

Reputation: 625017

$order->products[0]->data['attributes']['ID'][0]

Upvotes: 2

Related Questions