2015-11-06 83 views
0

我有一個JSON字符串與球員的統計數據,我想總的球員得分在這裏,玩家名稱是JSON如何計算球員得分與PHP

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

的例子。在這個例子中,我需要例如找到玩家1並計算所有點數 所以這就是它應該是正確的玩家1有233點+10點= 243點,$ player_name => $ totalpoints;

我試着用循環來做到這一點,但沒有成功。

+1

到目前爲止,您嘗試了什麼?另外,恕我直言,把它留到後端可能會更好。爲了有所幫助,在數組上使用'each' –

+0

使用'json_decode'然後運行一個循環..很可能 – adelowo

回答

0

循環遍歷數組,然後將它們添加..讓我假設數組名是$players的話..

$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; 

編輯:

這看起來像JSON代碼..所以首先對其進行解碼使用json_decode

+0

嘿phpfresher是不可能對'player1'進行harcode的? – Tim

+0

@Tim ..嗨,對不起,我沒有得到你..你是什麼意思由harcode? – phpfresher

+0

對不起,我的意思是硬代碼 – Tim

-1

下面是一個辦法做到這一點:

//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]; 
1

這應該適合你! 修訂

乾杯馬里奧

$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>"; 
} 
+0

感謝馬里奧,如何使用沒有硬編碼playerId? – Tim

+0

你想回來一個數組,他們都完成了嗎?每個玩家的總數是多少? – Mario

+0

更新了代碼與總結陣列輸出的球員值 – Mario

1

試試這個。通過使用這個,你可以得到你想要的結果。

$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); 
+0

嗨艾米特,謝謝,它的說total_score沒有定義 – Tim

+0

嘿蒂姆,我已經更新了ans。請檢查此.. –

+0

非常感謝@Amit,我添加了一個名爲「遊戲類型」的參數,我如何根據遊戲類型將玩家分組{ player_name「:」Player 1「, 」player_score「:10 }, – Tim