Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有这样的代码:
$result = $object->property[0];
但我想做类似的事情:
if ($a) { $property = 'blue' } else { $property = 'black'} $result = $object->$property[0];
但是,这给了我一个Cannot use string offset as an array错误。
Cannot use string offset as an array
任何指针(没有双关语)表示赞赏。
使用大括号:
$result = $object->{$property}[0];
$result = $object->{$property}