php多个数组 当date和customer的值一样 求和

发布于 2022-09-05 19:29:14 字数 1563 浏览 20 评论 0

$a = [
['date' => '2017-06-9', 'customer' => 1161, 'total' => 3, 'cg' => 1],
['date' => '2017-06-11', 'customer' => 1163, 'total' => 8, 'cg' => 3],
['date' => '2017-06-14', 'customer' => 1163, 'total' => 10, 'cg' => 5],
['date' => '2017-06-12', 'customer' => 1163, 'total' => 4, 'cg' => 2],

];
$b = [
['date' => '2017-06-10', 'customer' => 1161, 'total' => 3, 'cg' => 1],
['date' => '2017-06-11', 'customer' => 1163, 'total' => 9, 'cg' => 2],
['date' => '2017-06-13', 'customer' => 1165, 'total' => 6, 'cg' => 5],
['date' => '2017-06-11', 'customer' => 1165, 'total' => 5, 'cg' => 2]
];

//我写了个下面的数组处理方法,有更好的方法吗???

//代码开始

function merge_arr($myArray, $subArr){

$sArray = $zArray =  array();
foreach ($myArray as $k=>$subArray) {
   foreach ($subArr as $id=>$value) {
        if($subArray && $value && ($subArray['date'] == $value['date'] && $subArray['customer'] == $value['customer'])){
            $subArr[$id]['total'] += $subArray['total'];
            $subArr[$id]['cg'] += $subArray['cg'];
            unset($myArray[$k]);
        }
    }
 }
 return array_merge($myArray, $subArr);

}

print_r(merge_arr($myArray, $subArr));
//代码结束

//求最优处理上述数组的方法 我这个只能处理2组数组
//如果2组以上呢?我只能拿2组合并后的结果 再去处理第3个数组 有没优化方法

$mArray = merge_arr($myArray,$subArr);//前2组合并
$threeArray = merge_arr($mArray, $threeArr);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

独留℉清风醉 2022-09-12 19:29:15

        $a = [
            ['date' => '2017-06-9', 'customer' => 1161, 'total' => 3, 'cg' => 1],
            ['date' => '2017-06-11', 'customer' => 1163, 'total' => 8, 'cg' => 3],
            ['date' => '2017-06-14', 'customer' => 1163, 'total' => 10, 'cg' => 5],
            ['date' => '2017-06-12', 'customer' => 1163, 'total' => 4, 'cg' => 2],

        ];
        $b = [
            ['date' => '2017-06-10', 'customer' => 1161, 'total' => 3, 'cg' => 1],
            ['date' => '2017-06-11', 'customer' => 1163, 'total' => 9, 'cg' => 2],
            ['date' => '2017-06-13', 'customer' => 1165, 'total' => 6, 'cg' => 5],
            ['date' => '2017-06-11', 'customer' => 1165, 'total' => 5, 'cg' => 2],
        ];

        $c = [
            ['date' => '2017-06-10', 'customer' => 1161, 'total' => 43, 'cg' => 1],
            ['date' => '2017-06-11', 'customer' => 1163, 'total' => 9, 'cg' => 2],
            ['date' => '2017-06-13', 'customer' => 1165, 'total' => 6, 'cg' => 5],
            ['date' => '2017-06-11', 'customer' => 1165, 'total' => 5, 'cg' => 2],
        ];
        
        //...........

        //array_merge — 合并一个或多个数组
        // $temp = array_merge($a, $b, $c , $b , $d , $e , ...........);
        $temp = array_merge($a, $b, $c);

        $res = [];
        foreach ($temp as $item) {
            $year = $item['date'];
            $userId = $item['customer'];
            if(!isset($arr[$year.$userId])){
                $res[$year.$userId] =
                    [
                      'date' => $year,
                      'customer' => $userId,
                      'cg' => 0,
                      'total' => 0,
                    ];
            }
        }
        foreach ($temp as $tep) {
            if(isset($res[$tep['date'].$tep['customer']])){
                $res[$tep['date'].$tep['customer']]['total'] += $tep['total'];
                $res[$tep['date'].$tep['customer']]['cg'] += $tep['cg'];
            }
        }

        print_r(sort($res));
最偏执的依靠 2022-09-12 19:29:15

直接加:

    
    $a = [
        ['date' => '2017-06-9', 'customer' => 1161, 'total' => 3, 'cg' => 1],
        ['date' => '2017-06-11', 'customer' => 1163, 'total' => 8, 'cg' => 3],
        ['date' => '2017-06-14', 'customer' => 1163, 'total' => 10, 'cg' => 5],
        ['date' => '2017-06-12', 'customer' => 1163, 'total' => 4, 'cg' => 2],

    ];
    $b = [
        ['date' => '2017-06-10', 'customer' => 1161, 'total' => 3, 'cg' => 1],
        ['date' => '2017-06-11', 'customer' => 1163, 'total' => 9, 'cg' => 2],
        ['date' => '2017-06-13', 'customer' => 1165, 'total' => 6, 'cg' => 5],
        ['date' => '2017-06-11', 'customer' => 1165, 'total' => 5, 'cg' => 2],
    ];

    $c = [
        ['date' => '2017-06-10', 'customer' => 1161, 'total' => 43, 'cg' => 1],
        ['date' => '2017-06-11', 'customer' => 1163, 'total' => 9, 'cg' => 2],
        ['date' => '2017-06-13', 'customer' => 1165, 'total' => 6, 'cg' => 5],
        ['date' => '2017-06-11', 'customer' => 1165, 'total' => 5, 'cg' => 2],
    ];
    
    $arr = array_merge($a, $b, $c);
    $result = array();
    foreach ($arr as $val) {
        if(empty($val)) {
            continue;
        }
        $key = $val['date'] . '-' . $val['customer'];
        if(!isset($result[$key])) {
            $result[$key] = $val;
            continue;
        }
        $result[$key]['cg']    += (int)$val['cg'];
        $result[$key]['total'] += (int)$val['total'];
    }
    $result = array_values($result);
    var_dump("<pre>", $result);die;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文