PHP 以商品维度 去重user_id
$a = [
['name' => 'test', 'user_id' => 1, 'goods_id' => 2],
['name' => 'test', 'user_id' => 1, 'goods_id' => 2],
['name' => 'test', 'user_id' => 2, 'goods_id' => 2],
['name' => 'test', 'user_id' => 3, 'goods_id' => 2],
['name' => 'test', 'user_id' => 3, 'goods_id' => 3],
];
$data = [];
foreach ($a as $v) {
$key = $v['goods_id'];
!isset($data[$key]['pay_num']) && $data[$key]['pay_num'] = 0;
!isset($data[$key]['goods_id']) && $data[$key]['goods_id'] = 0;
!isset($data[$key]['user_id']) && $data[$key]['user_id'] = 0;
$data[$key]['goods_id'] = $v['goods_id'];
$data[$key]['user_id'] = $v['user_id'];
$data[$key]['pay_num'] = 1;
}
$data = array_values($data);
比如这组二维数组数据 统计下单人数 商品id 一样 用户一样 我想得到pay_num=1 该如何处理,
最后想得到的结果是
[
{
"pay_num": 3,
"goods_id": 2
},
{
"pay_num": 1,
"goods_id": 3
}
]
这玩意儿说简单也简单,只要把逻辑理清,怎么写就看自己了: