php从这种类型的关联数组中获取值
How can i get the firstName values from this array? its easy with print_r, but I want individual values
Array
(
[0] => stdClass Object
(
[id] => 106288917
[firstName] => xxxxx
[lastName] => yyyyy
)
[1] => stdClass Object
(
[id] => 106258850
[firstName] => zzzzz
[lastName] => ttttt
)
)
如何从这个数组中获取firstName值?使用print_r很容易,但我想要个别值 p>
Array
(
[0] => stdClass Object
(
[id] => 106288917
[firstName] => xxxxx
[ lastName] => yyyyy
)
[1] => stdClass对象
(
[id] => 106258850
[firstName] => zzzzz
[lastName] => ttttt
)
)
code> pre>
div>
How can i get the firstName values from this array? its easy with print_r, but I want individual values
You can do:
foreach($yourArray as $val){
echo $val->firstName;
}
Since your array contains objects eg stdClass
, you need to use ->
like shown above.
Try this (assume $a
is your array):
echo $a[0]->firstname;
Since you have an array of objects, you can either access each object by the array index or loop through the array to get each seperate object.
Once you have the object it self, you can simply access the first name property of the object.
Example of looping:
foreach ( $array as $object ) {
echo $object->firstname;
}
Where $array is the variable containing your array.
Example of accessing via array index:
echo $array[0]->firstname;
OR
$obj = $array[0];
echo $obj->firstname;
try this
foreach($x as $val)
{
echo $val->firstName;
}