web-dev-qa-db-ja.com

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と名前を取得する必要がありますが、このオブジェクトを反復処理するのに苦労しているようです。 TIA。値の配列に移動してからグループオブジェクトに移動する必要があることを理解していますが、オブジェクトと配列の間の遷移とforeachとインデックスベースの反復をつまずきます。

たとえば、私はこれを試しました:

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

オブジェクト全体を反復処理しますが、それも役に立たない

10 0 37
15
Mordechai
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>";
23
adeneo
function objectToArray( $data ) 
{
    if ( is_object( $data ) ) 
        $d = get_object_vars( $data );
}

最初に次のようにオブジェクトを配列に変換します。

$results = objectToArray( $results );

そして使う

foreach( $results as result ){... ...}
4
gus
foreach($res->values as $value) {
    print_r($value);
}

私はそれが古い投稿であることを知っていますが、他の目的のために:stdClassで作業するときはReflectionsを使用する必要があります:

  $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)



        }
0
eladcm