我正在嘗試使用PHP讀取XML文件。 XML文件是來自其他地方的報告。除非XML文件最終在'ReportItem'標籤內有多個具有相同名稱的標籤,否則一切正常。無法顯示數組的內容 - PHP XML
例如,當只有1個標籤時,該標籤的內容被正確顯示。但是當有多個標籤時,它會顯示'ARRAY(0x996c840)'。
我的XML看起來是這樣的:
<Scan>
<Report>
<ReportHost>
<ReportItem>
...
<see_also>http://google.com</see_also>
...
</ReportItem>
<ReportItem>
...
<see_also>http://google.com</see_also>
<see_also>http://google.com</see_also>
<see_also>http://google.com</see_also>
<see_also>http://google.com</see_also>
<see_also>http://google.com</see_also>
...
</ReportItem>
...
</ReportHost>
</Report>
</Scan>
我的代碼看起來是這樣的:
//Load the XML File
$xml = simplexml_load_file($path);
foreach ($xml->ReportHost as $reportHost)
{
$x = 1;
foreach ($reportHost->ReportItem as $reportItem)
{
...
$ARCHIVES['SEE_ALSO'][$x] = str_replace("\\n", '<br />', $reportItem->see_also);
...
}
}
我已經嘗試了很多不同的東西,但似乎沒有任何工作。即使只是試圖顯示它甚至不起作用。
我已經試過:
print_r($xml->xpath("//see_also"));
但仍然只給我的內容,如果只有1標籤,否則我會得到相同的「陣列(0x996c840)」的結果。
我也試過:
$z = 0;
while (isset($reportItem->see_also[$z]) AND ($reportItem->see_also[$z] != ""))
{
echo "See Also: " . $reportItem->see_also[$z] . "\n";
$z++;
}
但我總是得到同樣的事情。
我甚至用 'xml_to_array' 功能從xmlutils.php這是一個腳本,我從下載的嘗試:
http://pynej.blogspot.com/2010/02/convert-xml-to-array-and-array-to-xml.html
但它仍然總是顯示 '陣列(0x996c840)',而不是的內容。
我真的需要某人幫忙看看我做錯了什麼。在此先感謝您的幫助!
嘗試使用http://www.php.net/manual/de/class.simplexmliterator.php或許你在別的結果 – Gizzmo
@Gizzmo剛試過你的建議,仍然得到相同的結果。 [see_also] =>陣列 ( [0] => ARRAY(0x996c840) ) – Brendan