0
我試圖使用簡單的XML中的XPath元素來解析XML文檔。然而,這下面的腳本(當搜索的last.fm API中的條目「U2」)返回:PHP XML XPath - 停止重複節點
乘客乘客波諾波諾乘客U2和綠日乘客 波諾和U2樂隊綠日R.E.M.乘客Bono U2和Green Day R.E.M. INXS
正如你所看到的,有重複節點。有沒有辦法阻止重複/重複節點顯示?
(PHP代碼)
$xmlmusic = new SimpleXMLElement($result);
$releases = $xmlmusic->xpath('artist/similar/artist');
foreach ($releases as $artist) {
$artistResult .= $artist->name . PHP_EOL;
echo $artistResult;}
(XML文檔)
<?xml version="1.0" encoding="utf-8"?>
<lfm status="ok">
<artist>
<name>U2</name>
<mbid>704acdbb-1415-4782-b0b6-0596b8c55e46</mbid>
<url>http://www.last.fm/music/U2</url>
<image size="small">http://userserve-ak.last.fm/serve/34/107345.jpg</image>
<streamable>1</streamable>
<stats>
<listeners>2613654</listeners>
<playcount>96947986</playcount>
</stats>
<similar>
<artist>
<name>Passengers</name>
<url>http://www.last.fm/music/Passengers</url>
<image size="small">http://userserve-ak.last.fm/serve/34/4826014.jpg</image>
</artist>
它不返回任何結果,所以我嘗試了數組的打印: $ xmlmusic =新的SimpleXMLElement($結果); $ releases = $ xmlmusic-> xpath('artist/similar/artist'); $ seen = array();如果(!isset($ seen [$ artist-> name])){ $ US [$ artist-> name] = true; } } print_r($ see); 但是它返回一個空數組? – MattHeywood 2011-12-14 05:18:54