如何遍历嵌套的stdClass


How to iterate through a nested 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。我知道我需要先到达Values数组,然后到达group对象,但我在对象和数组之间的转换以及foreach与基于索引的迭代之间绊倒了。

例如,我尝试过这个:

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

它通过对象进行迭代,但也给了我无用的

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

如果你搜索iterate over stdclass,这是谷歌中排名第一的结果,回答标题中的问题可能会有所帮助:

您可以简单地使用foreach在stdclass上迭代:

$user = new 'stdClass();
$user->flag = 'red';
foreach ($user as $key => $value) {
   // $key is `flag`
   // $value is `red`
}
function objectToArray( $data ) 
{
    if ( is_object( $data ) ) 
        $d = get_object_vars( $data );
}

首先将对象转换为数组,如:

$results = objectToArray( $results );

并使用

foreach( $results as result ){... ...}

我知道这是一篇旧帖子,但为了其他人:使用stdClass时,应使用反射

  $obj = new ReflectionObject($object);
  $propeties = $obj->getProperties();
      foreach($properties as $property) {
        $name = $property->getName();  <-- this is the reflection class
        $value = $object->$name;       <--- $object is your original $object
            here you need to handle the result (store in array etc)

        }
foreach($res->values as $value) {
    print_r($value);
}