如何遍历嵌套的 stdClass?

新手上路,请多包涵

我有一个这样的对象:

 stdClass Object
(
    [_count] => 10
    [_start] => 0
    [_total] => 37
    [values] => Array
        (
            [0] => stdClass Object
                (
                    [_key] => 50180
                    [group] => stdClass Object
                        (
                            [id] => 50180
                            [name] => CriticalChain
                        )

                )

            [1] => stdClass Object
                (
                    [_key] => 2357895
                    [group] => stdClass Object
                        (
                            [id] => 2357895
                            [name] => Data Modeling
                        )

                )

            [2] => stdClass Object
                (
                    [_key] => 1992105
                    [group] => stdClass Object
                        (
                            [id] => 1992105
                            [name] => SQL Server Users in Israel
                        )

                )

            [3] => stdClass Object
                (
                    [_key] => 37988
                    [group] => stdClass Object
                        (
                            [id] => 37988
                            [name] => CDO/CIO/CTO Leadership Council
                        )

                )

            [4] => stdClass Object
                (
                    [_key] => 4024801
                    [group] => stdClass Object
                        (
                            [id] => 4024801
                            [name] => BiT-HR, BI & IT Placement Agency
                        )

                )

            [5] => stdClass Object
                (
                    [_key] => 37845
                    [group] => stdClass Object
                        (
                            [id] => 37845
                            [name] => Israel Technology Group
                        )

                )

            [6] => stdClass Object
                (
                    [_key] => 51464
                    [group] => stdClass Object
                        (
                            [id] => 51464
                            [name] => Israel DBA's
                        )

                )

            [7] => stdClass Object
                (
                    [_key] => 66097
                    [group] => stdClass Object
                        (
                            [id] => 66097
                            [name] => SQLDBA
                        )

                )

            [8] => stdClass Object
                (
                    [_key] => 4462353
                    [group] => stdClass Object
                        (
                            [id] => 4462353
                            [name] => Israel High-Tech Group
                        )

                )

            [9] => stdClass Object
                (
                    [_key] => 4203807
                    [group] => stdClass Object
                        (
                            [id] => 4203807
                            [name] => Microsoft Team Foundation Server
                        )

                )

        )

)

我需要在 HTML 表中获取 ID 和名称,但我似乎很难遍历此对象。 TIA。我知道我需要访问值数组,然后访问组对象,但我在对象和数组之间的转换以及 foreach 与基于索引的迭代之间绊倒了。

例如我试过这个:

 foreach ($res as $values) { print "\n"; print_r ($values); }

它遍历对象,但它也让我无用

10 0 37

原文由 Mordechai 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 1.2k
2 个回答
echo "<table>"

foreach ($object->values as $arr) {
    foreach ($arr as $obj) {
        $id   = $obj->group->id;
        $name = $obj->group->name;

        $html  = "<tr>";
        $html .=    "<td>Name : $name</td>";
        $html .=    "<td>Id   : $id</td>";
        $html .= "</tr>";
    }
}

echo "</table>";

原文由 adeneo 发布,翻译遵循 CC BY-SA 3.0 许可协议

如果您搜索 iterate over stdclass ,这是 Google 中的最高结果,回答标题中的问题可能会有所帮助:

您可以简单地使用 foreach 遍历 stdclass:

 $user = new \stdClass();
$user->flag = 'red';

foreach ($user as $key => $value) {
   // $key is `flag`
   // $value is `red`
}

原文由 Adam 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
logo
Stack Overflow 翻译
子站问答
访问
宣传栏