douyan4958 2013-05-13 22:07
浏览 58
已采纳

显示两个XML响应中都存在的子项

Still very new to php and XML and have been trying to find a solution to this for a while.

I am trying to compare two XML returns and only display the children that exist in both of them. To be specific i am using the steam api go get game libraries from two entered steam id's and i want to display the games that are owned by both users.

Instead of doing that it will now show all the games owned by user 1.

Here is the part that i am having problems with, $apinyckel is the API key and i am not including it in this post as it should not be shared.

$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;

Here is an example of what the XML structure looks like for one of the users.

<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>

Any help is appreciated, thanks!

  • 写回答

1条回答 默认 最新

  • doukang1962 2013-05-13 22:15
    关注

    To get array of messages you can use xpath:

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

    , and then loop through array to compare them.

    EDIT:

    Code for loop, extract names from array and intersect them:

    $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">';
    }
    

    Hope it helps.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值