PHP Echo数组值循环(不显示数据)

时间:2013-03-05 18:12:53

标签: php

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

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

它只是没有返回任何数据。

我猜这个循环可能对这个数组结构有误吗?

如何获取此数组的数据?

1 个答案:

答案 0 :(得分:2)

$list对象的数组,因此您需要两个循环和适当的语法。 e.g:

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