Nate
Nate

Reputation: 28334

Can't directly access array inside of StdClass object?

I'm having trouble accessing an array inside of an StdObject. The object looks like this in my debugger:

$obj = {stClass}[9]
  1234 = {array} [28]
    0 = "some text"
    1 = false
    2 = true
    3 = ""
  ...

It seems like I should be able to access elements in the array like this:

$tmp = 1234;
echo $Obj->$tmp[0]

But I get Notice: Undefined property: stdClass::$5

However, when I do this:

print_r($Obj->$tmp);

It prints out the array just fine.

Why am I unable to access an element in the array, even though I can print out the array?

Upvotes: 1

Views: 504

Answers (1)

Aziz Saleh
Aziz Saleh

Reputation: 2707

What PHP version are you one, something like this would work for me:

echo $Obj->{$tmp}[0]

On PHP >= 5.1

Reasoning behind this I think is because $tmp[0] is translated before accessing the object as oppose to translating $tmp, then access the object via $tmp

Upvotes: 2

Related Questions