1$collection = collect([1, 2, 3, 4]);
2
3$filtered = $collection->filter(function ($value, $key) {
4 return $value > 2;
5});
6
7$filtered->all();
8
9// [3, 4]
1$collection = collect([
2 [
3 'name' => 'John',
4 'department' => 'Sales',
5 'email' => 'john@example.com',
6 ],
7 [
8 'name' => 'Jane',
9 'department' => 'Marketing',
10 'email' => 'jane@example.com',
11 ]
12]);
13
14$keyed = $collection->mapWithKeys(function ($item) {
15 return [$item['email'] => $item['name']];
16});
17
18$keyed->all();
19
20/*
21 [
22 'john@example.com' => 'John',
23 'jane@example.com' => 'Jane',
24 ]
25*/
1$collection = collect([
2 [
3 'name' => 'John',
4 'department' => 'Sales',
5 'email' => 'john@example.com'
6 ],
7 [
8 'name' => 'Jane',
9 'department' => 'Marketing',
10 'email' => 'jane@example.com'
11 ]
12]);
13
14$keyed = $collection->mapWithKeys(function ($item) {
15 return [$item['email'] => $item['name']];
16});
17
18$keyed->all();
19
20/*
21 [
22 'john@example.com' => 'John',
23 'jane@example.com' => 'Jane',
24 ]
25*/