@larissa
In Laravel, you can use the partition
method on collections to separate the items into two arrays based on a given callback.
Here's an example of how you can use the partition
method in Laravel:
1 2 3 4 5 6 7 8 9 10 |
use IlluminateSupportCollection; $collection = collect([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); list($evenNumbers, $oddNumbers) = $collection->partition(function ($value, $key) { return $value % 2 == 0; }); // $evenNumbers will contain [2, 4, 6, 8, 10] // $oddNumbers will contain [1, 3, 5, 7, 9] |
In this example, we first create a collection with the numbers 1 to 10. We then use the partition
method to separate the numbers into two arrays: even numbers and odd numbers. The callback function passed to the partition
method determines which array the item will be placed in based on whether the value is even or odd.
You can use the resulting arrays as needed in your application. The partition
method is a convenient way to split a collection into two separate arrays based on a condition.