我在获取这些对象中的数组时遇到了一些问题。当我print_r()时,打印下面的代码。 $ message_object是对象的名称。使用SimpleXMLElement从对象获取数组
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
)
)
)
我试图让ID阵列出在foreach对象:
foreach($message_object->data->id AS $id) {
print_r($id);
}
下发送回复:
SimpleXMLElement Object ([0] => 65233) SimpleXMLElement Object ([0] => 65234)
我如何获得价值[0]还是我正在讨论这个错误?有没有办法循环通过结果并获得对象键?
我试图echo $ id [0]但它没有返回任何结果。
尝试循环这个第一:的foreach($ message_object为K => $ VAL $),那么你应该能够添加一个if语句来检查$ k = 0。 – MrTechie
您是否尝试回显$ id而不是$ id [0]? – kennypu
当我print_r($ id)它给了我SimpleXMLElement对象([0] => 65233),因此theroy $ id [0]应该= 65233. – Philip