Jon
Jon

Reputation: 3

Comparing json arrays

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.

Upvotes: 0

Views: 1441

Answers (2)

Barmar
Barmar

Reputation: 781833

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

Upvotes: 1

Paul Roub
Paul Roub

Reputation: 36458

$games->PlayedGames->Title doesn't actually refer to anything. You'll need to loop through the game lists and pull the values out one at a time.

Something like:

$playedGames = array();

foreach ($games->PlayedGames as $game)
  checkGame($game);
foreach ($fgames->PlayedGames as $game)
  checkGame($game);

foreach ($playedGames as $id => $game)
{
  if ($game->playerCount == 2)
  {
    // whatever you like with $game->Title and $game->BoxArt
  }
}

function checkGame($game)
{
  global $games;

  $id = $game->Id;

  if (array_key_exists($id, $games))
     $games[$id]->playerCount++;
  else
  {
     $games[$id] = array( 'playerCount' => 1, 
        'Title' => $game->Title,
        'BoxArt' => $game->BoxArt
       );
  }
}

Upvotes: 0

Related Questions