phparraysgroupingcumulative-sumyearmonth

Group and sum values in associative array based on year substring in keys


I am trying to sum all of the yearly amounts based on an array with keys in a month-year ("M y") date format.

$array_list = [
    "Jan 2016" => 2,
    "Feb 2016" => 4,
    "Mar 2016" => 2,
    "Apr 2016" => 0,
    "Jan 2017" => 9,
    "Feb 2017" => 2,
    "Mar 2017" => 5,
    "Jan 2018" => 4,
    "Feb 2018" => 6
];

Code:

<table border='1' cellpadding='5' cellspancing='3'>
    <tr>
        <td>SN</td>
        <td>Month </td>
        <td>Year </td>
        <td>is new? </td>
        <td>Data</td>
        <td>Total</td>
    </tr>
    <?php 
    $prev_year = 0; 
    $i = 1;
    $total_val = 0; 
    foreach ($array_list as $key => $val):
        $row_span = 0; 
        $MonthYear_label    = explode(" ", $key); 
        $year_label = $MonthYear_label[1];
        $month_label = $MonthYear_label[0];
        $curr_year = $year_label;

        if ($curr_year == $prev_year) {
            $str = "same";
            $total_val = $total_val + $val; 
        } else {
            if ($i == 1) {
                //skip for first time.. first iterator 
            } else {
                echo "<tr>
                          <td colspan='6'>&nbsp;&nbsp;</td>
                      </tr>";
            }
            $str = "--new--";
            $total_val = $val;      
       }
       echo '<tr> 
                 <td>' . $i . '</td>
                 <td>' . $month_label . '</td>
                 <td>' . $year_label . ' </td>
                 <td>' . $str . '</td>
                 <td>' . $val . '</td>
                 <td><b>' . $total_val . '</b></td>
             </tr>';
       $i++; 
       $prev_year = $curr_year; 
    endforeach; 
    ?> 
</table>

Current Output:

current output

What i expect ? By adding those data year wise..

desired output

Notes: all data appears year-wise & data column represents the total sum of data


Solution

  •     <?php 
        $array_list= array("Jan 2016"=>2, "Feb 2016"=>4, "Mar 2016"=>2,"Apr 2016"=>0, "Jan 2017"=>9,"Feb 2017"=>2,"Mar 2017"=>5,"Jan 2018"=>4,"Feb 2018"=>6);
        ?>
        <table border='1' cellpadding='5' cellspancing='3'>
            <tr>
                <td>SN</td>
                <td>Month/Year</td>
                <td>Data</td>
            </tr>
    
            <?php 
            $array = array();
            foreach($array_list as $key => $value):
                $MonthYear_label    = explode(" ",$key); 
                $year_label         = $MonthYear_label[1];
                $month_label        = $MonthYear_label[0];
                $curr_year          = $year_label; 
                $array[$curr_year]['count'] =  $array[$curr_year]['count'] + $value;
            endforeach; 
            $i = 1;
            foreach ($array as $key => $value) {
                echo '<tr> 
                <td>'.$i.'</td>
                <td>'.$key.'</td>
                <td>'.$value['count'].' </td>
                </td>';
            }
            ?>
        </table>