从包含stdClass PHP对象的数组中获取值


Getting values from arrays containing stdClass PHP objects

我从Facebook Graph API获取信息,该API返回一个包含多个stdClass对象的数组。我可以很容易地阅读下面示例中的"顶级"项目,如$myGraph['id']=1231111193。

有人能告诉我如何从stdClass对象中获取数据吗,例如下面用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
                    )
<?php
foreach($mainArray['education'] as $edObj) 
{
     echo $edObj->school->name;
}

没什么特别的。如果您有一个名为$foo的std对象,并且它有一个$bar成员,那么您将其称为$foo->bar。示例:

foreach ($mainArray["education"] as $value) {
    echo $value->school->name;
}