I have a json string with players statistics and I want to get total player score with that player name here is an example of the json
<code>{
"Players": [
{
"playerId": 1,
"player_name": "Player 1",
"player_score": 10
"game_type": "action"
},
{
"playerId": 2,
"player_name": "Player 1",
"player_score": 120
"game_type": "action"
},
{
"playerId": 1,
"player_name": "Player 1",
"player_score": 233
"game_type": "action"
},
{
"playerId": "n",
"player_name": "Player n",
"player_score": "n"
}
]
}
</code>
In this example I need to find player 1 for example and calculate all points so this is what it should be right player 1 has 233 points + 10 points = 243 points, $player_name => $totalpoints;
I've trying with loops to do that but with no success.
Loop through the array and then add them.. let me suppose the array name is $players
, then..
$total_score = 0;
foreach($players as $key=>$value)
{
if($value['player_name']=='player1')
{
$total_score +=$value['player_score'];
}
}
echo "PLAYER's TOTAL SCORE = ".$total_score;
EDIT:
This looks like JSON code.. so first decode it using json_decode
Here is a way to do it :
//Convert json to php array
$listPlayers = json_decode({
"Players": [
{
"playerId": 1,
"player_name": "Player 1",
"player_score": 10
},
{
"playerId": 2,
"player_name": "Player 1",
"player_score": 120
},
{
"playerId": 1,
"player_name": "Player 1",
"player_score": 233
},
{
"playerId": "n",
"player_name": "Player n",
"player_score": "n"
}
]
});
//Sum score for each player id
$totalByPlayers = array();
foreach($listPlayers as $player)
{
//If player doesnt exists yet, we create it
if(false === isset($totalByPlayers[$player['playerId']]))
{
$totalByPlayers[$player['playerId']] = 0;
}
$totalByPlayers[$player['playerId']] += $player['player_score'];
}
//Then for score of player one :
echo $totalByPlayers[1];
this should work for you! UPDATED
Cheers Mario
$json = '[{"playerId": 1,"player_name": "Player 1","player_score": 10}, {"playerId": 2,"player_name": "Player 1","player_score": 20},{"playerId": 1,"player_name": "Player 1","player_score": 233},{"playerId": "3","player_name": "Player 3","player_score": "3"}]';
$array = json_decode( $json, true );
$totalcount = array();
foreach ($array as $key => $value){
if (!empty($totalcount[$value['playerId']])) {
$totalcount[$value['playerId']] += $value['player_score'];
}
else
{
array_push($totalcount, $value['playerId'],$value['player_score'] );
}
}
foreach ($totalcount as $key => $value) {
echo "Player ".$key." total=".$value."<br>";
}
Try this. By using this you can get the result as you want.
$player_array = json_decode($json_string); // put your json string variable
$palyer_array_total = array();
foreach($player_array as $player){
$palyer_array_total[$player['playerId']]['name'] = $player['player_name'];
if(isset($palyer_array_total[$player['playerId']]['total_score'])){
$palyer_array_total[$player['playerId']]['total_score'] = $palyer_array_total[$player['playerId']]['total_score']+$player['player_score'];
}else{
$palyer_array_total[$player['playerId']]['total_score'] = $player['player_score'];
}
}
echo "<pre>"; print_r($palyer_array_total);