假设我有一个这样的收藏集:
$collection = [
App\User {name: 'Bob', age: 20},
App\User {name: 'Joe', age: 22},
App\User {name: 'Lisa', age: 20}
]
,我将它们分组为$collection->groupBy('age')
。我得到这个结果:
[
"20" => [
App\User {name: 'Bob', age: 20},
App\User {name: 'Lisa', age: 20},
],
"21" => [
App\User {name: 'Joe', age: 22}
]
]
我想删除关联键,仅使用索引数组,如下所示:
[
[
App\User {name: 'Bob', age: 20},
App\User {name: 'Lisa', age: 20},
],
[
App\User {name: 'Joe', age: 22]
]
]
这可能与Laravel收集方法有关吗?预先感谢您的任何指点!
答案 0 :(得分:3)
使用array_values
:https://www.php.net/manual/en/function.array-values.php
array_values($collection->groupBy('age'));
更多Laravel方式
$collection->groupBy('age')->values();