如何用php

时间:2015-11-06 10:08:23

标签: php json laravel

我有一个带有球员统计数据的json字符串,我希望获得该玩家名称的总玩家得分,这里是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>

在这个例子中,我需要找到例如球员1并计算所有点数 所以这应该是正确的球员1有233分+ 10分= 243分,$ player_name =&gt; $ totalpoints;

我尝试使用循环来做到这一点,但没有成功。

4 个答案:

答案 0 :(得分: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>";
}

答案 1 :(得分: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);

答案 2 :(得分: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

对其进行解码

答案 3 :(得分:-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];