0

我在使用 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
            )

    )

)

但是这个循环会引发以下错误。

试图获取非对象的属性

请让我知道如何 从上述格式中读取产品属性值

谢谢大家

4

1 回答 1

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
             }
         }
于 2013-10-09T03:04:45.453 回答