0
如何訪問此assoc數組?PHP parse assoc。數組或XML
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,爲什麼呢? }
// OK,讓我們嘗試另一種方式......
$items = toArray($src); //googled function - see at the bottom
print_r($items);
//打印結果 - 看到頁面頂部assoc命令陣列
//以及如何存取權限在這個訂單ID (fck)assoc數組???
// ------------------------------------------
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;
}
很多感謝任何幫助!
$ items ['order-id'] [0] and $ items ['order-id'] [1] –
好吧,這似乎工作...以及如何使動態集合(迭代器)如果更多爲了-ID(S)? – noh
[歡迎使用StackOverflow,請參閱此處如何使用本網站](http://stackoverflow.com/about) – Prix