Array
(
[0] => Array
(
[oid] => 190
[color] =>
[goods_name] => 测试要
[goods_num] => 1
[goods_price] => 0.01
[id] => 44
[img] => /Public/Uploads/2017-12-13/5a30fa5763bdb.jpg
[user_openid] => oWWUP0dieOLSHl3CzjyHIcIdhtVo
[order_number] => 20181513226463
[tid] => 92
[status] => 1
[tuiid] =>
[aid] =>
[gid] =>
[corm_time] => 1514862004
)
[1] => Array
(
[oid] => 191
[color] =>
[goods_name] => A5笔记本子
[goods_num] => 2
[goods_price] => 0.01
[id] => 43
[img] => /Public/Uploads/2017-12-06/5a27add5325d1.jpg
[user_openid] => oWWUP0dieOLSHl3CzjyHIcIdhtVo
[order_number] => 20181513241259
[tid] => 93
[status] => 1
[tuiid] =>
[aid] =>
[gid] =>
[corm_time] => 1514862004
)
[2] => Array
(
[oid] => 193
[color] =>
[goods_name] => 测试要
[goods_num] => 1
[goods_price] => 0.01
[id] => 46
[img] => /Public/Uploads/2017-12-13/5a30fa5763bdb.jpg
[user_openid] => oWWUP0dieOLSHl3CzjyHIcIdhtVo
[order_number] => 20181513241259
[tid] => 93
[status] => 1
[tuiid] =>
[aid] =>
[gid] =>
[corm_time] => 1514862004
)
)
如上数组是 $a ;
我需求出上面数组的 goods_num 之和 最终组合成下面这个样的数组,请大神们帮个忙吧!
Array
(
[0] => Array
(
[name] => 测试要
[number] => 2
)
[1] => Array
(
[name] => A5笔记本子
[number] => 2
)
)
完全没有头绪,也不知道怎么搞。。。。反正就是各种懵逼了!!!~
我通过直接在数据库计算,出现这样的结果
Array
(
[0] => Array
(
[name] => 测试要
[number] => 2
)
[1] => Array
(
[name] => A5笔记本子
[number] => 2
)
[2] => Array
(
[name] => 测试要
[number] => 2
)
)
但是去重又是个问题了。我实在不知道咋搞了。。还是得求大神啊!!!
4 回答
慕田峪4524236
TA贡献1875条经验 获得超5个赞
假设你的商品表为goods
select goods_name as name,sum(goods_num) as number from goods group by name;
小怪兽爱吃肉
TA贡献1852条经验 获得超1个赞
$arr = [
['goods_name' => 'a', 'goods_num' => 1],
['goods_name' => 'b', 'goods_num' => 1],
['goods_name' => 'c', 'goods_num' => 1],
['goods_name' => 'a', 'goods_num' => 2],
['goods_name' => 'a', 'goods_num' => 3],
['goods_name' => 'a', 'goods_num' => 5],
['goods_name' => 'a', 'goods_num' => 8],
['goods_name' => 'b', 'goods_num' => 8],
['goods_name' => 'd', 'goods_num' => 9],
];
$arr = array_reduce($arr, function($group, $item) {
$group[$item['goods_name']][] = $item['goods_num'];
return $group;
}, []);
$arr = array_map(function ($i) {
return array_sum($i);
}, $arr);
print_r($arr);
- 4 回答
- 0 关注
- 408 浏览
添加回答
举报
0/150
提交
取消