2

I have code like :

$result = $object->property[0];

but I want to do something like :

if ($a) {  
  $property = 'blue' }  
else {  
  $property = 'black'}  
$result = $object->$property[0];

However this give me a Cannot use string offset as an array error.

Any pointers (no pun intended) appreciated.

1
  • 1
    Remove the leading $ on the $property[0]; Commented Nov 19, 2012 at 9:58

2 Answers 2

6

Use braces:

$result = $object->{$property}[0];
Sign up to request clarification or add additional context in comments.

Comments

0

$result = $object->{$property}

Comments

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.