在Php中循环stdClass对象的问题

时间:2013-10-09 02:59:16

标签: php json

我在使用php循环json对象时遇到了奇怪的问题。这是我的数据格式

stdClass Object
(
[userform] => Array
    (
        [0] => stdClass Object
            (
                [id] => 69
                [product] => testuser
            )

        [1] => stdClass Object
            (
                [id] => 70
                [product] => testuser
            )

        [2] => stdClass Object
            (
                [id] => 71
                [product] => testuser
            )

        [3] => stdClass Object
            (
                [id] => 72
                [product] => testuser
            )

        [4] => stdClass Object
            (
                [id] => 73
                [product] => testuser
            )

        [5] => stdClass Object
            (
                [id] => 74
                [product] => testuser
            )

        [6] => stdClass Object
            (
                [id] => 75
                [product] => testuser
            )

        [7] => stdClass Object
            (
                [id] => 76
                [product] => testuser
            )

        [8] => stdClass Object
            (
                [id] => 77
                [product] => testuser
            )

        [9] => stdClass Object
            (
                [id] => 78
                [product] => testuser
            )

        [10] => stdClass Object
            (
                [id] => 79
                [product] => testuser
            )

        [11] => stdClass Object
            (
                [id] => 80
                [product] => testuser
            )

        [12] => stdClass Object
            (
                [id] => 81
                [product] => testuser
            )

        [13] => stdClass Object
            (
                [id] => 82
                [product] => testuser
            )

        [14] => stdClass Object
            (
                [id] => 83
                [product] => testuser
            )

        [15] => stdClass Object
            (
                [id] => 84
                [product] => testuser
            )

        [16] => stdClass Object
            (
                [id] => 85
                [product] => testuser
            )

        [17] => stdClass Object
            (
                [id] => 86
                [product] => testuser
            )

        [18] => stdClass Object
            (
                [id] => 87
                [product] => testuser
            )

        [19] => stdClass Object
            (
                [id] => 88
                [product] => testuser
            )

        [20] => stdClass Object
            (
                [id] => 89
                [product] => testuser
            )

    )

)

但是这个循环会引发跟随错误。

尝试在

中获取非对象的属性

请告诉我如何阅读上述格式的产品属性值

全部谢谢

1 个答案:

答案 0 :(得分:0)

该foreach应该在$data->userform上,而不仅仅是$data

foreach($data->userform as $key=>$row){
             if(isset($row->product)){
               print_r($key);
               print_r($row->product);  
             }else{
               //product property is not set
             }
         }