1

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
        )
)
matt
  • 2,312
  • 5
  • 34
  • 57
  • Do you want to know how to *access* the names, or how to transform this into an array of names? – Jon Feb 23 '12 at 11:01

4 Answers4

3

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;
j4kes
  • 374
  • 2
  • 13
  • Thanks Sarfaz, been using SO for quite a while, but never registered though. – j4kes Feb 23 '12 at 11:59
  • The wealth of knowledge floats here, being active users, I have learned more than I did working professionally :) – Sarfraz Feb 23 '12 at 12:00
2

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.

Sarfraz
  • 377,238
  • 77
  • 533
  • 578
2

try this

foreach($x as $val)
{
echo $val->firstName;
}
waseem
  • 167
  • 1
  • 4
0

Try this (assume $a is your array):

echo $a[0]->firstname;
Minras
  • 4,136
  • 4
  • 18
  • 18