简体   繁体   English

通过API接收的数据进行存储/移动

[英]Storing/moving through data received from API

Im trying to store and then use the data I receive from API (in PHP ). 我试图存储然后使用从API(在PHP中 )接收到的数据。

However, it needs to be stored in a good way so it can be easily accessed. 但是,它需要以良好的方式存储,以便可以轻松访问。

An example result from api: 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"
}]}

How would i store the following information in php so that later I can access "leaguePoints" of "2012131"? 我将如何在php中存储以下信息,以便以后可以访问“ 2012131”的“ leaguePoints”? I would appreciate some example code but it's not necessary. 我会喜欢一些示例代码,但这不是必需的。

edit: forgot to mention that I tried creating arrays and splitting up data using 编辑:忘了提到我尝试使用创建数组和拆分数据

$text_line = explode( "," , $text_line );

but couldn't get right result. 但无法得到正确的结果。

You can use json_decode() , and then store the object/array in session . 您可以使用json_decode() ,然后将对象/数组存储在session中

Converting that JSON response into array in PHP: 将该JSON响应转换为PHP中的数组:

$arrayResponse = json_decode($apiResponse,true);
print_r($arrayResponse);

This will result in: 这将导致:

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

In order to save that array for later use, you could do: 为了保存该数组供以后使用,您可以执行以下操作:

session_start();
$_SESSION['apiResponse'] = $arrayResopnse;

Then on another page you can access the playerOrTeamName like this: 然后在另一个页面上,您可以像这样访问playerOrTeamName

session_start();
$playerOrTeamName = $_SESSION['apiResponse']['2012131']['0']['entries']['0']['playerOrTeamName'];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM