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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.