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

时间:2015-03-01 22:26:03

标签: php arrays facebook stdclass

我从Facebook Graph API获取信息,该信息返回包含多个stdClass对象的数组。我可以轻松阅读" top"级别项目,例如下面示例中的$ myGraph [' id'] = 123111193。

有谁能告诉我如何从stdClass对象获取数据,例如以下使用print_r()创建的示例中的School Name?

    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
                    )

2 个答案:

答案 0 :(得分:1)

<?php

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

答案 1 :(得分:1)

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

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