简体   繁体   中英

Storing/moving through data received from API

Im trying to store and then use the data I receive from API (in PHP ).

However, it needs to be stored in a good way so it can be easily accessed.

An example result from 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"? 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 .

Converting that JSON response into array in 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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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