Search code examples
phparraysjsonassociative

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

Solution

  • 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.