ForsakenHound ForsakenHound - 1 month ago 10
JSON Question

Getting a array in JSON PHP

I am trying to figure out how to get from a bunch of arrays the highest "tier": "VALUE" and get access to the entire array its in. In this case the highest "tier": "DIAMOND".

List of Tiers (Highest to lowest):

CHALLENGER

MASTER

DIAMOND

PLATINUM

GOLD

SILVER

BRONZE

I don't know how I can get each arrays data to find the highest tier and use that tier to gain access to the other data according to that array.

Here is the JSON file

{
"29161162": [
{
"name": "Graves's Inquisitors",
"tier": "DIAMOND",
"queue": "RANKED_SOLO_5x5",
"entries": [
{
"playerOrTeamId": "29161162",
"playerOrTeamName": "ForsakenHound",
"division": "III",
"leaguePoints": 50,
"wins": 234,
"losses": 190,
"playstyle": "NONE",
"isHotStreak": false,
"isVeteran": false,
"isFreshBlood": false,
"isInactive": false
}
]
},
{
"name": "Leona's Dervish",
"tier": "PLATINUM",
"queue": "RANKED_TEAM_5x5",
"entries": [
{
"playerOrTeamId": "TEAM-4e132820-393f-11e6-8b2a-c81f66daeaa4",
"playerOrTeamName": "Raging Turtles Alpha",
"division": "II",
"leaguePoints": 40,
"wins": 12,
"losses": 3,
"playstyle": "NONE",
"isHotStreak": false,
"isVeteran": false,
"isFreshBlood": false,
"isInactive": false
}
]
},
{
"name": "Darius's Ritualists",
"tier": "PLATINUM",
"queue": "RANKED_TEAM_5x5",
"entries": [
{
"playerOrTeamId": "TEAM-612b20d0-59c3-11e6-ae27-c81f66daeaa4",
"playerOrTeamName": "Raging Turtles Reborn",
"division": "II",
"leaguePoints": 5,
"wins": 8,
"losses": 4,
"playstyle": "NONE",
"isHotStreak": false,
"isVeteran": false,
"isFreshBlood": false,
"isInactive": false
}
]
},
{
"name": "Kayle's Lancers",
"tier": "PLATINUM",
"queue": "RANKED_TEAM_5x5",
"entries": [
{
"playerOrTeamId": "TEAM-989e11c5-8c05-11e6-9e23-c81f66dd7106",
"playerOrTeamName": "Raging turtles",
"division": "I",
"leaguePoints": 0,
"wins": 5,
"losses": 0,
"playstyle": "NONE",
"isHotStreak": false,
"isVeteran": false,
"isFreshBlood": false,
"isInactive": false
}
]
}
]
}

Answer

You can group them by value of the tier like this:

$levels = explode(',', 'BRONZE,SILVER,GOLD,PLATINUM,DIAMOND,MASTER,CHALLENGER');

$tier_data = array();
foreach ($jsona as $unknown_key_id=>&$users) {
  foreach ($users as &$user) {
    $tier_value = array_search($user->tier, $levels);
    $tier_data[$tier_value][] = $user;
  }
}

die('<pre>'.print_r($tier_data,true));