如何访问此assoc数组?
Array
(
[order-id] => Array
(
[0] => 1
[1] => 2
)
)
作为XML解析的结果
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE request SYSTEM "http://shits.com/wtf.dtd">
<request version="0.5">
<order-states-request>
<order-ids>
<order-id>1</order-id>
<order-id>2</order-id>
...
</order-ids>
</order-states-request>
</request>
$body = file_get_contents('php://input');
$xml = simplexml_load_string($body);
$src = $xml->{'order-states-request'}->{'order-ids'};
foreach ($src as $order) {
echo ' ID:'.$order->{'order-id'};
//不工作 - 仅回复ID:1,为什么? }
//好吧,让我们尝试另一种方式......
$items = toArray($src); //googled function - see at the bottom
print_r($items);
//打印结果 - 请参阅顶部的关联数组
//以及如何访问此(fck)assoc数组中的订单ID ???
// ------------------------------------------
function toArray(SimpleXMLElement $xml) {
$array = (array)$xml;
foreach ( array_slice($array, 0) as $key => $value ) {
if ( $value instanceof SimpleXMLElement ) {
$array[$key] = empty($value) ? NULL : toArray($value);
}
}
return $array;
}
非常感谢任何帮助!
答案 0 :(得分:1)
你想要的是:
$body = file_get_contents('php://input');
$xml = simplexml_load_string($body);
$src = $xml->{'order-states-request'}->{'order-ids'}->{'order-id'};
foreach ($src as $id)
{
echo ' ID:', $id, "\n";
}
你的代码会发生什么,你正试图循环:
$xml->{'order-states-request'}->{'order-ids'}
这不是您想要的array
,order-id
就像您在转储中看到的那样:
Array
(
[order-id] => Array