1

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?

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

2 Comments

That worked for me. I've never encountered this behavior before and it seems really strange. Is this a bug with PHP?
@Nate I wouldn't say it is a bug with PHP, just the way the syntax works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.