数组重组求助~~

发布于 2022-09-11 18:28:15 字数 2981 浏览 30 评论 0

下面这堆数组wid,gid全部相同的,要求Num累加,不同的不操作直接显示,数组去重如何实现?

Array
(
    [0] => Array
        (
            [id] => 1
            [no] => WF20170001
            [wid] => 1
            [gid] => 1
            [num] => 4
            [price] => 2.55
            [amount] => 10.20
            [buyer] => 顾文君
            [invoice] => 123456789
            [date] => 2017-03-28
            [remarks] => 测试数据
            [cid] => pc001
            [class] => 办公用品
            [name] => 笔记本电脑
            [type] => Acer Aspire E1-410G
            [unit] => 台
            [warehouse_name] => 办公用品仓库
        )

    [1] => Array
        (
            [id] => 3
            [no] => WF20170001
            [wid] => 1
            [gid] => 5
            [num] => 5
            [price] => 100.00
            [amount] => 500.00
            [buyer] => 顾文君
            [invoice] => 1231241
            [date] => 2017-04-14
            [remarks] => 入库测试
            [cid] => tf001
            [class] => 办公用品
            [name] => 打印机碳粉
            [type] => 88A
            [unit] => 支
            [warehouse_name] => 办公用品仓库
        )

    [2] => Array
        (
            [id] => 4
            [no] => WF20170001
            [wid] => 1
            [gid] => 2
            [num] => 1
            [price] => 4500.00
            [amount] => 4500.00
            [buyer] => 张秀珍
            [invoice] => 4558895
            [date] => 2017-04-09
            [remarks] => 入库测试
            [cid] => pc002
            [class] => 办公用品
            [name] => 笔记本电脑
            [type] => Acer Aspire E5-571G-58WT
            [unit] => 台
            [warehouse_name] => 办公用品仓库
        )

    [3] => Array
        (
            [id] => 5
            [no] => WF20170002
            [wid] => 1
            [gid] => 3
            [num] => 1
            [price] => 88.00
            [amount] => 88.00
            [buyer] => 顾文君
            [invoice] => 556789
            [date] => 2017-04-09
            [remarks] => 测试数据
            [cid] => ow
            [class] => 办公用品
            [name] => 万用表
            [type] => 万用表
            [unit] => 台
            [warehouse_name] => 办公用品仓库
        )

    [4] => Array
        (
            [id] => 12
            [no] => WF20170001
            [wid] => 1
            [gid] => 1
            [num] => 1
            [price] => 2.55
            [amount] => 2.55
            [buyer] => 顾文君
            [invoice] => 53412312
            [date] => 2017-06-05
            [remarks] => 无
            [cid] => pc001
            [class] => 办公用品
            [name] => 笔记本电脑
            [type] => Acer Aspire E1-410G
            [unit] => 台
            [warehouse_name] => 办公用品仓库
        )

)

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

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

发布评论

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

评论(2

梦里兽 2022-09-18 18:28:15
$temp = [];
foreach($data as $item) { // $data 为待处理数据
    if (isset($temp[$item['wid']][$item['gid']])) {
        $temp[$item['wid']][$item['gid']]['num'] += $item['num'];
    } else {
        $temp[$item['wid']][$item['gid']] = $item;
    }
}

$result = [];
foreach ($temp as $wid => $val1) {
    foreach ($val1 as $gid => $val2) {
        $result[] = $val2; 
    }
}
var_dump ($result);
↙温凉少女 2022-09-18 18:28:15

我的理解其实你现在做的就是 php层面 实现mysql group by

//伪代码思路

$tmp_arr = [];
foreach 
    $tmp_arr[$v['wid'].$v['gid']]  +=$v['num']; //自己判断下数据件是否存在,不存在就初始化0 ,还要把重复的key 删除掉,加在第一个上面 这个临时数据 判断下 下哪个key 需要删除
    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文