比较JSON阵列 [英] Comparing json arrays

查看:130
本文介绍了比较JSON阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用的Xbox API返回给定用户gameplayed信息的网站上。它返回JSON格式的结果:

  {
数据:{
玩家代号:主要纳尔逊
Gamerpic:HTTPS:\\ / \\ / avatar-ssl.xboxlive.com \\ /头像\\ /主要%20Nelson \\ /avatarpic-l.png
GameCount:779,
TotalEarnedGamerScore:63147,
TotalPossibleGamerScore:593465,
TotalEarnedAchievements:3429,
TotalPossibleAchievements:25498,
TotalPercentCompleted:13,
PlayedGames:[
{
ID:1297287449,
标题:光环4,
URL:HTTP:\\ / \\ / marketplace.xbox.com \\ / EN-US \\ /标题\\ / 1297287449
BoxArt:HTTPS:\\ / \\ / www.xboxleaders.com \\ / IMG \\ / boxart \\ /1297287449-small.jpg
LargeBoxArt:HTTPS:\\ / \\ / www.xboxleaders.com \\ / IMG \\ / boxart \\ /1297287449-large.jpg
EarnedGamerScore:705,
PossibleGamerScore:1500年,
EarnedAchievements:38,
PossibleAchievements:67,
PercentageCompleted:56.7,
LastPlayed:1363751187
},
]
},
统计:OK,
在:3.818,
Authed:假,
AuthedAs:空
}

我试图创建一个数组,将检查两个不同用户的信息,以便我可以创建一个for循环,只显示这两个用户都玩过游戏的标题和BoxArt(无论完成的百分比或其他变量)。我试过以下code:

 < PHP
$玩家代号=使用isset($ _ GET ['标签'])? $ _GET ['标签']:空;
$ friendtag =使用isset($ _ GET ['FTAG'])? $ _GET ['FTAG']:空;
$ gamertag2 = urlen code($玩家代号);
$ friendtag2 = urlen code($ friendtag);
//获取游戏信息
$游戏= json_de code(url_get_contents('http://www.xboxleaders.com/api/games.json?gamertag='.$gamertag2));
$游戏= $游戏 - >数据;
$ fgames = json_de code(url_get_contents('http://www.xboxleaders.com/api/games.json?gamertag='.$friendtag2));
$ fgames = $ fgames->数据;
?>< PHP
$数组1 =数组($游戏 - > PlayedGames->标题);
$数组2 =阵列($ fgames-> PlayedGames->标题);
$结果= array_intersect($数组1,$数组2);
的print_r($结果);
?>

但是,这总是返回一个空集。当我使用$游戏 - > PlayedGames的阵列,它合并两个数组并显示每个所有数据。我真的AP preciate手段,以比较两个用户阵列,这将使我的比赛的两个用户有共同的只是标题和Boxart。


解决方案

 函数GAME_TITLE($游戏){
    返回$游戏 - >标题;
}
$数组1 = array_map('GAME_TITLE',$游戏 - > PlayedGames);
$数组2 = array_map('GAME_TITLE',$ fgames-> PlayedGames);
$结果= array_intersect($数组1,$数组2);
的print_r($结果);

I'm working on a website utilizing an Xbox API that returns gameplayed information for a given user. It returns results in the json format:

{
"Data": {
"Gamertag": "Major Nelson",
"Gamerpic": "https:\/\/avatar-ssl.xboxlive.com\/avatar\/Major%20Nelson\/avatarpic-l.png",
"GameCount": 779,
"TotalEarnedGamerScore": 63147,
"TotalPossibleGamerScore": 593465,
"TotalEarnedAchievements": 3429,
"TotalPossibleAchievements": 25498,
"TotalPercentCompleted": 13,
"PlayedGames": [
{
"Id": 1297287449,
"Title": "Halo 4",
"Url": "http:\/\/marketplace.xbox.com\/en-US\/Title\/1297287449",
"BoxArt": "https:\/\/www.xboxleaders.com\/img\/boxart\/1297287449-small.jpg",
"LargeBoxArt": "https:\/\/www.xboxleaders.com\/img\/boxart\/1297287449-large.jpg",
"EarnedGamerScore": 705,
"PossibleGamerScore": 1500,
"EarnedAchievements": 38,
"PossibleAchievements": 67,
"PercentageCompleted": 56.7,
"LastPlayed": 1363751187
},
]
},
"Stat": "ok",
"In": 3.818,
"Authed": "false",
"AuthedAs": null
}

I'm trying to create an array that will check the info of two different users so I can create a for loop to display only the game Titles and BoxArt both users have played (regardless of percentage complete or other variables). I've tried the following code:

<?php 
$gamertag = isset($_GET['tag']) ? $_GET['tag'] : null;
$friendtag = isset($_GET['ftag']) ? $_GET['ftag'] : null;
$gamertag2 = urlencode($gamertag);
$friendtag2 = urlencode ($friendtag);
// Get game information
$games = json_decode(url_get_contents('http://www.xboxleaders.com/api/games.json?gamertag='.$gamertag2));
$games = $games->Data;
$fgames = json_decode(url_get_contents('http://www.xboxleaders.com/api/games.json?gamertag='.$friendtag2));
$fgames = $fgames->Data;
?>

<?php 
$array1 = array($games->PlayedGames->Title);
$array2 = array($fgames->PlayedGames->Title);
$result = array_intersect($array1, $array2);
print_r($result);
?>

But this always returns an empty set. When I just use $games->PlayedGames for the arrays, it merges the two array and displays all the data in each. I would really appreciate a means to compare two users array that will give me just the Titles and Boxart of the games two users have in common.

解决方案

function game_title ($game) {
    return $game->Title;
}
$array1 = array_map('game_title', $games->PlayedGames);
$array2 = array_map('game_title', $fgames->PlayedGames);
$result = array_intersect($array1, $array2);
print_r($result);

这篇关于比较JSON阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆