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

preferences:
88.42 ms | 1360 KiB | 5 Q