遍历 PHP 中的 stdClass 对象 [英] iterating through a stdClass object in PHP

查看:30
本文介绍了遍历 PHP 中的 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 和 name,但我似乎很难遍历这个对象.TIA.我知道我需要访问 Values Array,然后访问 group 对象,但是我在对象和数组之间的转换以及 foreach 与基于索引的迭代之间绊倒了.

I need to get the id and name in an HTML table, but I seem to have a hard time iterating through this object. TIA. I understand that I need to get to the Values Array, and then to the group object, but I trip over the transitions between object and array and foreach vs index based iteration.

例如我试过这个:

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

它遍历对象,但它也给了我无用的

It iterates trough the object, but it also gives me useless

10 0 37

推荐答案

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>";

这篇关于遍历 PHP 中的 stdClass 对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆