Search code examples
phparrayslaravelsumgrouping

Group data in 2d array by one column, and use another column for summing


Array
(
    [0] => Array
        (        
            [player_name] => AC
            [round] => 1              
            [winlose] => 10
            [game_title] => First
        )

    [1] => Array
        (
            [player_name] => M3
            [round] => 1              
            [winlose] => -50
            [game_title] => First 
        )

    [2] => Array
        (
            [player_name] => M2
            [round] => 2             
            [winlose] => -50
            [game_title] => Second 
        )

    [3] => Array
        (
            [player_name] => M1
            [round] => 2              
            [winlose] => -150
            [game_title] => Second 
        )

    [4] => Array
        (
            [player_name] => M5
            [round] => 1              
            [winlose] => -50
            [game_title] => First 
        )

    [5] => Array
        (
            [player_name] => M7
            [round] => 2              
            [winlose] => 50
            [game_title] => Second 
        )
)

My code

foreach ($getAllRound as $key => $value) {
    $negative = 0;
    $positive = 0;
   
    if (strpos($value['winlose'], '-') !== false) {
        $negative += $value['winlose'];
    } else{
        $positive += $value['winlose'];
    }
    
    $total = $positive + $negative;

    $dataa[$value['round']] = array(
                'round'    => $value['round'],
                'result'   => $value['game_title'],
                'positive' => $positive,
                'negative' => $negative,
                'total'    => $total,
            );
}

** final result should be**

Array
    (
        [1] => Array
            (
                [round] => 1
                [positive] => 10
                [negative] => -100
                [total] => -90
            )
    
        [2] => Array
            (
                [round] => 2
                [positive] => 50
                [negative] => -200
                [total] => -150
            )
    )

Question: The above code unable to get the final result, by right it will loop the array and group as round then count total positive and negative value in every round and the total of the round. Does anyone able to help on this ya?

It's able to group as round 1 and 2, but the positive and negative value was wrong.


Solution

  • Have you heard of Laravel Collection?

    There are many functions you can use, such as groupBy, map, and sum in your case :

    $data = collect($yourArray)
        ->groupBy('round')
        ->map(function($item, $round){
            $positive = collect($item)->map(function($item){ return $item['winlose'] > 0 ? $item['winlose'] : 0; })->sum();
            $negative = collect($item)->map(function($item){ return $item['winlose'] < 0 ? $item['winlose'] : 0; })->sum();
    
            return [
                'round' => $round,
                'positive' => $positive,
                'negative' => $negative,
                'total' => $positive + $negative
            ];
        });
    
    dd($data->toArray());
    

    Output :

    array:2 [
      1 => array:4 [
        "round" => 1
        "positive" => 10
        "negative" => -100
        "total" => -90
      ]
      2 => array:4 [
        "round" => 2
        "positive" => 50
        "negative" => -200
        "total" => -150
      ]
    ]