0

我有一个数组......这是结构/数据:

array(1) { 
    [0]=> object(SimpleXMLElement)#1 (18) 
        { 
            ["data_123"]=> object(SimpleXMLElement)#3 (29) 

            { 
                ["field1"]=> string(7) "123" 
                ["field2"]=> string(2) "10" 
                ["field3"]=> string(19) "2013-03-05 17:00:00" 
                ["field4"]=> string(19) "2013-03-05 18:00:00" 

            } 

                ["data_234"]=> object(SimpleXMLElement)#4 (29) 

            { 

                ["field1"]=> string(7) "234" 
                ["field2"]=> string(2) "10" 
                ["field3"]=> string(19) "2013-03-05 17:40:00" 
                ["field4"]=> string(19) "2013-03-05 18:10:00" 

            } 

        } 

    }

我正在尝试创建一个循环来显示数据,但没有显示任何内容:

foreach ($result as $key => $list) {
   echo "key.: " . $key . "\n";
   echo "field1: " . $list['field1'] . "\n";
   echo "field2: " . $list['field2'] . "\n";
}

它只是不返回任何数据。

我猜这个数组结构的循环可能是错误的?

我怎样才能得到这个数组的回显数据?

4

1 回答 1

2

$list是一个对象数组,因此您需要两个循环和适当的语法。例如:

foreach($list as $objects) {
    foreach($objects as $key => $obj) {
        echo "key.: " . $key . "\n";
        echo $obj->field1 . "\n";
        echo $obj->field2 . "\n";
        echo $obj->field3 . "\n";
        echo $obj->field4 . "\n";
    }
}
于 2013-03-05T18:21:01.700 回答