比较JSON阵列

编程入门 行业动态 更新时间:2024-10-23 17:35:56
本文介绍了比较JSON阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

{数据:{玩家代号:主要纳尔逊Gamerpic:HTTPS:\\ / \\ / avatar-ssl.xboxlive \\ /头像\\ /主要%20Nelson \\ /avatarpic-l.pngGameCount:779,TotalEarnedGamerScore:63147,TotalPossibleGamerScore:593465,TotalEarnedAchievements:3429,TotalPossibleAchievements:25498,TotalPercentCompleted:13,PlayedGames:[{ID:1297287449,标题:光环4,URL:HTTP:\\ / \\ / marketplace.xbox \\ / EN-US \\ /标题\\ / 1297287449BoxArt:HTTPS:\\ / \\ / www.xboxleaders \\ / IMG \\ / boxart \\ /1297287449-small.jpgLargeBoxArt:HTTPS:\\ / \\ / www.xboxleaders \\ / IMG \\ / boxart \\ /1297287449-large.jpgEarnedGamerScore: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('www.xboxleaders/api/games.json?gamertag='.$gamertag2));$游戏= $游戏 - >数据;$ fgames = json_de code(url_get_contents('www.xboxleaders/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\/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\/en-US\/Title\/1297287449", "BoxArt": "https:\/\/www.xboxleaders\/img\/boxart\/1297287449-small.jpg", "LargeBoxArt": "https:\/\/www.xboxleaders\/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('www.xboxleaders/api/games.json?gamertag='.$gamertag2)); $games = $games->Data; $fgames = json_decode(url_get_contents('www.xboxleaders/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阵列

本文发布于:2023-10-24 14:44:51,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1524211.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:阵列   JSON

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!