在PHPsimplexml_load_string給出不一致的XML標籤
$str = '<?xml version="1.0"?><toplevel><CompleteSuggestion><suggestion data="google"/></CompleteSuggestion><CompleteSuggestion><suggestion data="google maps"/></CompleteSuggestion><CompleteSuggestion><suggestion data="google translate"/></CompleteSuggestion><CompleteSuggestion><suggestion data="google docs"/></CompleteSuggestion><CompleteSuggestion><suggestion data="google drive"/></CompleteSuggestion><CompleteSuggestion><suggestion data="google classroom"/></CompleteSuggestion><CompleteSuggestion><suggestion data="google news"/></CompleteSuggestion><CompleteSuggestion><suggestion data="google earth"/></CompleteSuggestion><CompleteSuggestion><suggestion data="google flights"/></CompleteSuggestion><CompleteSuggestion><suggestion data="google calendar"/></CompleteSuggestion></toplevel>';
$xml = simplexml_load_string($str);
$data = json_decode(json_encode($xml),true);
echo "ARRAY: <pre>";
var_dump($data);
與簡單的XML解析XML字符串時,在上述情況下,我得到不一致的XML標籤,我使用的是從谷歌的自動完成功能,並將其與內建議作出迴應CompleteSuggestions標記。看看php數組,我得到一個包含10個項目的CompleteSuggestion數組。前9個鍵有[0]鍵,而最後一個鍵有'[建議]鍵。這對我來說很奇怪。
任何一個人知道爲什麼簡單的XML做到這一點?我怎樣才能解決它,或解析它,因爲我的foreach得到了一個偏移錯誤。謝謝!
您正在使用什麼版本的PHP?上5.6的結果爲[ 「CompleteSuggestion」=> [/ * 10個*元件/]的數組] –
PHP 5.5。我也越來越10個元素的數組但這是每個元素與不同的密鑰 – Dan
陣列中的問題,爲什麼,爲什麼,爲什麼,你的SimpleXML的對象轉換爲一個數組,並扔掉了SimpleXML的所有有用的位API? 'foreach($ xml-> CompleteSuggestion爲$ comp_suggestion){$ suggestion = $ comp_suggestion-> suggestion; echo $ suggestion ['data']; }' – IMSoP