我试图存储然后使用从API接收的数据(在 PHP 中)。
但是,它需要以良好的方式存储,以便于访问。
api的示例结果:
{"2012131": [{
"queue": "RANKED_SOLO_5x5",
"name": "Diana's Assassins",
"entries": [{
"leaguePoints": 40,
"isFreshBlood": true,
"isHotStreak": false,
"division": "V",
"isInactive": false,
"isVeteran": false,
"playerOrTeamName": "Myname",
"playerOrTeamId": "2012131",
"wins": 200
}],
"tier": "PLATINUM"
}]}
我如何在php中存储以下信息,以便以后我可以访问" leaguePoints" of" 2012131"?我会很感激一些示例代码,但它没有必要。
编辑:忘了提到我尝试使用
创建数组和拆分数据$text_line = explode( "," , $text_line );
但无法获得正确的结果。
答案 0 :(得分:0)
您可以使用json_decode(),然后将对象/数组存储在session中。
在PHP中将该JSON响应转换为数组:
$arrayResponse = json_decode($apiResponse,true);
print_r($arrayResponse);
这将导致:
Array
(
[2012131] => Array
(
[0] => Array
(
[queue] => RANKED_SOLO_5x5
[name] => Diana's Assassins
[entries] => Array
(
[0] => Array
(
[leaguePoints] => 40
[isFreshBlood] => 1
[isHotStreak] =>
[division] => V
[isInactive] =>
[isVeteran] =>
[playerOrTeamName] => Myname
[playerOrTeamId] => 2012131
[wins] => 200
)
)
[tier] => PLATINUM
)
)
)
为了保存该数组供以后使用,您可以这样做:
session_start();
$_SESSION['apiResponse'] = $arrayResopnse;
然后在另一个页面上,您可以像这样访问playerOrTeamName
:
session_start();
$playerOrTeamName = $_SESSION['apiResponse']['2012131']['0']['entries']['0']['playerOrTeamName'];