简体   繁体   中英

php get values from this type of associative array

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
        )
)

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;

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

foreach($x as $val)
{
echo $val->firstName;
}

试试这个(假设$a是您的数组):

echo $a[0]->firstname;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM