Sekoul Sekoul - 4 months ago 13
PHP Question

PHP - refer to stdclass object within array

CONTEXT

I'm trying to refer to value below:

Array (
[0] => stdClass Object (
[label] => Nancy Adler
[attributes] => Array (
[class] => sf-level-0 sf-item-14
)
[value] => adler-nancy
[depth] => 0
[count] => 3
)
[1] =>... etc.


QUESTION

Assuming the array is called $array, what would be the right format for this? I've tried
array[0]->['value']
, but it does not work.

Answer

Tough to say with your formatting. but:

$array[0]->value

The first element in the array is of type stdClass Object, so to access its members, you use the object accessor -> rather than the array syntax.

PS: Kudos to Marc for making things legible with his edit.