【发布时间】:2012-12-22 01:57:17
【问题描述】:
我在获取这些对象中的数组时遇到了一些问题。当我 print_r() 时,会打印以下代码。 $message_object 是对象的名称。
SimpleXMLElement Object
(
[header] => SimpleXMLElement Object
(
[responsetime] => 2012-12-22T14:10:09+00:00
)
[data] => SimpleXMLElement Object
(
[id] => Array
(
[0] => 65233
[1] => 65234
)
[account] => Array
(
[0] => 20992
[1] => 20992
)
[shortcode] => Array
(
[0] => 3255
[1] => 3255
)
[received] => Array
(
[0] => 2012-12-22T11:04:30+00:00
[1] => 2012-12-22T11:31:08+00:00
)
[from] => Array
(
[0] => 6121843347
[1] => 6121820166
)
[cnt] => Array
(
[0] => 24
[1] => 25
)
[message] => Array
(
[0] => Go tramping wellington 11-30
[1] => Go drinking Matakana 2pm
)
)
)
我正在尝试使用 foreach 从对象中获取 id 数组:
foreach($message_object->data->id AS $id) {
print_r($id);
}
发送如下回复:
SimpleXMLElement Object ( [0] => 65233 ) SimpleXMLElement Object ( [0] => 65234 )
我如何获得 [0] 的值,或者我是不是搞错了?有没有办法循环遍历结果并获取对象键?
我尝试回显 $id[0],但没有返回任何结果。
【问题讨论】:
-
先尝试循环:foreach($message_object AS $k=>$val) 然后你应该可以添加一个 if 语句来检查 $k = 0。
-
您是否尝试只回显 $id 而不是 $id[0]?
-
当我 print_r($id) 它给了我 SimpleXMLElement 对象 ( [0] => 65233 ) 所以理论上 $id[0] 应该 = 65233。