phparraysmultidimensional-arraysumgrouping

Group a 2d array by year-month of a date column, add column of counts and create a subarray from a column of ids in each group


Here is my code so far

$tmp_array = array();
foreach ($cms as $key => $val) {
    $cDate = date('Ym', strtotime($val['day_date']));
    $tmp_ids[] = $val['id'];

    if (array_key_exists($cDate, $tmp_array)) {
        $tmp_array[$cDate]['new_visitors'] += $val['new_visitors'];
        $tmp_array[$cDate]['ids'] = $tmp_ids;
    } else {
        $tmp_array[$cDate]['new_visitors'] = $val['new_visitors'];
        $tmp_array[$cDate]['ids'] = $tmp_ids;
    }
}

Its output is coming like this:

Array
(
    [202001] => Array
        (
            [new_visitors] => 797
            [ids] => Array
                (
                    [0] => 31
                    [1] => 32
                )

        )

    [202002] => Array
        (
            [new_visitors] => 461
            [ids] => Array
                (
                    [0] => 31
                    [1] => 32
                    [2] => 33
                    [3] => 34
                )

        )

)

but I want the result array like this:

Array
(
    [202001] => Array
        (
            [new_visitors] => 797
            [ids] => Array
                (
                    [0] => 31
                    [1] => 32
                )

        )

    [202002] => Array
        (
            [new_visitors] => 461
            [ids] => Array
                (
                    [0] => 33
                    [1] => 34
                )

        )

)

What am I doing wrong in my code?

The ids are basically primary key of the table and the "new_visitors" are count of the visitor those who visit on my site.

Here is my $cms array.

Array
(
    [0] => Array
        (
            [id] => 31
            [day_date] => 2020-01-30 00:00:00
            [new_visitors] => 459
        )

    [1] => Array
        (
            [id] => 32
            [day_date] => 2020-01-31 00:00:00
            [new_visitors] => 338
        )

    [2] => Array
        (
            [id] => 33
            [day_date] => 2020-02-01 00:00:00
            [new_visitors] => 242
        )

    [3] => Array
        (
            [id] => 34
            [day_date] => 2020-02-02 00:00:00
            [new_visitors] => 219
        )
)

Solution

  • Thank you misorude, your comment worked

      $tmp_array = array();
                    foreach ($cms as $key => $val) {
                        $cDate = date('Ym', strtotime($val['day_date']));
                        if (array_key_exists($cDate, $tmp_array)) {
                            $tmp_array[$cDate]['new_visitors'] += $val['new_visitors'];
                            $tmp_array[$cDate]['ids'][] = $val['id'];
                        } else {
                            $tmp_array[$cDate]['new_visitors'] = $val['new_visitors'];
                            $tmp_array[$cDate]['ids'][] = $val['id'];
                        }
                    }