Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute:

I'm obtaining information from the Facebook Graph API that returns an array that contains several stdClass Objects. I can easily read the "top" level items such as $myGraph['id'] = 123111193 in the example below.

Can anyone show me how to get data from the stdClass Objects, for example School Name in the following example created with print_r()?

    Array
    (
        [id] => 123111193
        [education] => Array
    (
        [0] => stdClass Object
            (
                [school] => stdClass Object
                    (
                        [id] => 108177302537907
                        [name] => State College Area High School
                    )

                [type] => High School
                [year] => stdClass Object
                    (
                        [id] => 117615364954534
                        [name] => 1975
                    )

            )

        [1] => stdClass Object
            (
                [concentration] => Array
                    (
                        [0] => stdClass Object
                            (
                                [id] => 193334910691838
                                [name] => Individual and Family Studies
                            )

                    )

                [school] => stdClass Object
                    (
                        [id] => 113618111985274
                        [name] => Pennsylvania State University
                    )
share|improve this question
    
to get the first school name this should work: $array['education'][0]->school->name – skroczek Mar 1 at 22:30
    
already discussed here – check Mar 2 at 1:16
    
$array['education'][0]->school->name works PERFECTLY!!! Thanks VERY much!! – Fred Nelson Mar 2 at 2:41

2 Answers 2

up vote 1 down vote accepted
<?php

foreach($mainArray['education'] as $edObj) 
{
     echo $edObj->school->name;
}
share|improve this answer
    
This is not working with my FB object. The test "is_object" is never true since the type is an array. If I change it to "is_array" it evaluates as true yet there is no output. Any ideas would greatly help - Thanks!!!! – Fred Nelson Mar 2 at 2:43
    
Sorry about that -- I had just woken up earlier when I posted this. Revised. – Patrick Webster Mar 2 at 8:25
    
While this may answer the question it’s always a good idea to put some text in your answer to explain what you're doing. Read how to write a good answer. – jurgemaister Mar 2 at 9:22

Nothing fancy. If you have an std object called $foo and it has a $bar member, then you refer to it as $foo->bar. Example:

foreach ($mainArray["education"] as $value) {
    echo $value->school->name;
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.