3v4l.org

run code in 500+ PHP versions simultaneously
<?php $getAllRound = [ [ 'player_name' => 'AC', 'round' => 1, 'winlose' => 10, 'game_title' => 'First', ], [ 'player_name' => 'M3', 'round' => 1, 'winlose' => -50, 'game_title' => 'First', ], [ 'player_name' => 'M2', 'round' => 2, 'winlose' => -50, 'game_title' => 'Second', ], [ 'player_name' => 'M1', 'round' => 2, 'winlose' => -150, 'game_title' => 'Second', ], [ 'player_name' => 'M5', 'round' => 2, 'winlose' => -50, 'game_title' => 'First', ], [ 'player_name' => 'M7', 'round' => 2, 'winlose' => 50, 'game_title' => 'Second', ], ]; $roundsExist = array(); $outputArray = array(); foreach($getAllRound as $playerRound){ if(!in_array($playerRound['round'], $roundsExist)){ $roundsExist[] = $playerRound['round']; $outputArray[$playerRound['round']]['round'] = array( 'round' => $playerRound['round'], 'positive' => 0, 'negative' => 0, 'total' => 0 ); if($playerRound['winlose'] <= 0){ $outputArray[$playerRound['round']]['round']['negative'] += $playerRound['winlose']; }else{ $outputArray[$playerRound['round']]['round']['positive'] += $playerRound['winlose']; } $outputArray[$playerRound['round']]['round']['total'] += $playerRound['winlose']; } } print_r($outputArray);
Output for 7.4.0 - 7.4.33, 8.0.1 - 8.0.30, 8.1.0 - 8.1.34, 8.2.0 - 8.2.30, 8.3.0 - 8.3.30, 8.4.1 - 8.4.21, 8.5.0 - 8.5.7
Array ( [1] => Array ( [round] => Array ( [round] => 1 [positive] => 10 [negative] => 0 [total] => 10 ) ) [2] => Array ( [round] => Array ( [round] => 2 [positive] => 0 [negative] => -50 [total] => -50 ) ) )

preferences:
96.08 ms | 1361 KiB | 4 Q