Array( [0] => Array ( [id] => 1 [member_id] => 8 [total] => 5 [mainroomid] => 2 ) [1] => Array ( [id] => 2 [member_id] => 11 [total] => 2 [mainroomid] => 2 ) [2] => Array ( [id] => 3 [member_id] => 8 [total] => 8 [mainroomid] => 2 ) [3] => Array ( [id] => 4 [member_id] => 11 [total] => 3 [mainroomid] => 2 ) [4] => Array ( [id] => 5 [member_id] => 29 [total] => 4 [mainroomid] => 2 ) [5] => Array ( [id] => 6 [member_id] => 8 [total] => 5 [mainroomid] => 1 ) [6] => Array ( [id] => 7 [member_id] => 11 [total] => 3 [mainroomid] => 1 ))上面的数据是从MySQL数据库返回的,我的逻辑被困在这里,我想相应地循环数据并将数据重新排列到一个新数组中。是否意味着所有相同的会员id我需要将所有的总数相加并根据mainroomid分开。任何人都可以帮忙解决这个问题:(?下面是我需要显示的数据。
1 回答
函数式编程
TA贡献1807条经验 获得超9个赞
您可以使用 laravel 的集合通过sortBy 和groupBy方法轻松完成此操作。
// Lets say $data contains the data you listed above.
$sorted = collect($data)->sortBy('member_id')->groupBy('mainroomid')->values()->toArray();
- 1 回答
- 0 关注
- 137 浏览
添加回答
举报
0/150
提交
取消