2013-05-13 29 views
0

對於PHP和XML還是很新的,並且一直試圖找到解決方案。顯示存在於兩個XML響應中的兒童

我想比較兩個XML返回,只顯示兩個存在的子項。具體來說,我正在使用蒸汽API從兩個輸入的蒸汽ID獲取遊戲庫,我想顯示兩個用戶擁有的遊戲。

而不是做,它現在將顯示用戶1.

這裏所擁有的所有的遊戲的是,我有問題的部分,$ apinyckel是API密鑰,我不包括它在這個崗位因爲它不應該被共享。

$steamid2 = $_SESSION['steamid2']; 
$steamid3 = $_SESSION['steamid3']; 

    //user1 
    $url4 = "http://api.steampowered.com/IPlayerService/GetOwnedGames/v0001/?key=$apinyckel&steamid=$steamid2&format=xml&&include_appinfo=1"; 
    $data4 = file_get_contents($url4); 
    $xml4 = simplexml_load_string($data4); 

    //user2 
    $url5 = "http://api.steampowered.com/IPlayerService/GetOwnedGames/v0001/?key=$apinyckel&steamid=$steamid3&format=xml&&include_appinfo=1"; 
    $data5 = file_get_contents($url5); 
    $xml5 = simplexml_load_string($data5); 


    foreach ($xml4->games->children() as $item2): 
    if ($xml4->games->child->appid === $xml5->games->child->appid) { 
    ?><img src="http://media.steampowered.com/steamcommunity/public/images/apps/<?echo htmlentities($item2->appid);?>/<?echo htmlentities($item2->img_logo_url);?>.jpg"><? 
    } 
    endforeach; 

以下是其中一個用戶的XML結構的示例。

<response> 
<game_count>6</game_count> 
<games> 
<message> 
<appid>520</appid> 
<name>Team Fortress 2 Beta</name> 
<img_icon_url>e3f595a92552da3d664ad00277fad2107345f743</img_icon_url> 
<img_logo_url>6f6d22ab0c357d9f02a11f76ff35797e4ccdf19f</img_logo_url> 
<has_community_visible_stats>true</has_community_visible_stats> 
</message> 
<message> 
<appid>440</appid> 
<name>Team Fortress 2</name> 
<playtime_forever>30659</playtime_forever> 
<img_icon_url>e3f595a92552da3d664ad00277fad2107345f743</img_icon_url> 
<img_logo_url>07385eb55b5ba974aebbe74d3c99626bda7920b8</img_logo_url> 
<has_community_visible_stats>true</has_community_visible_stats> 
</message> 
<message> 
<appid>550</appid> 
<name>Left 4 Dead 2</name> 
<playtime_forever>1822</playtime_forever> 
<img_icon_url>7d5a243f9500d2f8467312822f8af2a2928777ed</img_icon_url> 
<img_logo_url>205863cc21e751a576d6fff851984b3170684142</img_logo_url> 
<has_community_visible_stats>true</has_community_visible_stats> 
</message> 
<message> 
<appid>223530</appid> 
<name>Left 4 Dead 2 Beta</name> 
<img_icon_url/> 
<img_logo_url/> 
</message> 
<message> 
<appid>320</appid> 
<name>Half-Life 2: Deathmatch</name> 
<playtime_forever>70</playtime_forever> 
<img_icon_url>795e85364189511f4990861b578084deef086cb1</img_icon_url> 
<img_logo_url>6dd9f66771300f2252d411e50739a1ceae9e5b30</img_logo_url> 
<has_community_visible_stats>true</has_community_visible_stats> 
</message> 
<message> 
<appid>340</appid> 
<name>Half-Life 2: Lost Coast</name> 
<img_icon_url>795e85364189511f4990861b578084deef086cb1</img_icon_url> 
<img_logo_url>867cce5c4f37d5ed4aeffb57c60e220ddffe4134</img_logo_url> 
</message> 
</games> 
</response> 

任何幫助表示讚賞,謝謝!

回答

1

要得到消息的數組,你可以使用XPath:

$xml1 = $xml1->xpath('//message'); 
$xml2 = $xml2->xpath('//message'); 

,然後遍歷數組對它們進行比較。

編輯:

代碼迴路,從陣列提取物的名稱和他們相交:

$messages1 = array(); 
array_map(
    function($message) use (&$messages1) { 
     $messages1[(string) $message->appid] = (array) $message; 
    }, 
    $xml1 
); 

$messages2 = array(); 
array_map(
    function($message) use (&$messages2) { 
     $messages2[(string) $message->appid] = (array) $message; 
    }, 
    $xml2 
); 


$result = array_intersect_key($messages1, $messages2); 

foreach ($result as $value) { 
    echo '<img src="http://media.steampowered.com/steamcommunity/public/images/apps/'. htmlentities($value['appid']) .'/'. htmlentities($value['img_logo_url']) .'.jpg">'; 
} 

希望它能幫助。

+0

@Johan Snitt,補充糾正循環 – Danijel 2013-05-13 23:31:08

+0

謝謝!明天會試用。 – 2013-05-14 01:24:53

+0

嗯,我試着這樣做,但出了點問題。 $ value ['img_logo_url']和$ value ['appid']顯示爲正在比較的遊戲名稱的第一個字母。因此,img網址最終就像「http://media.steampowered.com/steamcommunity/public/images/apps/T/T.jpg」。 – 2013-05-14 10:50:06