按日期列的年月对二维数组进行分组,添加计数列并从每个组中的 id 列创建一个子数组

问题描述 投票:0回答:3

这是我到目前为止的代码

$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;
    }
}

它的输出是这样的:

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
                )

        )

)

但我想要这样的结果数组:

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

        )

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

        )

)

我的代码做错了什么?

id 基本上是表的主键,“new_visitors”是访问我网站的访客数量。

这是我的 $cms 数组。

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
        )
)
php arrays multidimensional-array sum grouping
3个回答
1
投票

谢谢您misorude,您的评论有效了

  $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'];
                    }
                }

0
投票
$arr = [
    ['id' => 31 ,'day_date'=> "2020-01-30 00:00:00", 'new_visitors' => 459],
    ['id' => 32 ,'day_date'=> "2020-01-31 00:00:00", 'new_visitors' => 338],
    ['id' => 33 ,'day_date'=> "2020-02-01 00:00:00", 'new_visitors' => 242],
    ['id' => 34 ,'day_date'=> "2020-02-02 00:00:00", 'new_visitors' => 219]
];
echo "<pre>";
print_r($arr);
$data = [];
$add_visitor = 0;
$day_date = '';
foreach($arr as $arr_value){

    if(date('Ym', strtotime($arr_value['day_date'])) != $day_date){
        $add_visitor = 0;
    }
    $add_visitor += $arr_value['new_visitors'];
    $data[date('Ym', strtotime($arr_value['day_date']))]['new_visitors'] = $add_visitor;
    $data[date('Ym', strtotime($arr_value['day_date']))]['ids'][] = $arr_value['id'];
    $day_date = date('Ym', strtotime($arr_value['day_date']));
}

print_r($data);

0
投票

此任务的重要注意事项是:

  • 不要重复自己并且
  • 不要进行不必要的函数调用

第一次遇到年月,建立基值。

然后将新的访客计数添加到组中,并将 id 推送到组中。

代码:(演示

$result = [];
foreach ($array as $row) {
    $ym = date('Ym', strtotime($row['day_date']));
    $result[$ym] ??= ['new_visitors' => 0, 'ids' => []];
    $result[$ym]['new_visitors'] += $row['new_visitors'];
    $result[$ym]['ids'][] = $row['id'];
}
var_export($result);

输出:

array (
  202001 => 
  array (
    'new_visitors' => 797,
    'ids' => 
    array (
      0 => 31,
      1 => 32,
    ),
  ),
  202002 => 
  array (
    'new_visitors' => 461,
    'ids' => 
    array (
      0 => 33,
      1 => 34,
    ),
  ),
)
© www.soinside.com 2019 - 2024. All rights reserved.