-
Notifications
You must be signed in to change notification settings - Fork 11k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[5.1] Add split method into collection #10889
Conversation
ping @JosephSilber |
How is this different than the existing |
This splits the collection into N pieces. |
@@ -820,6 +820,25 @@ public function splice($offset, $length = null, $replacement = []) | |||
} | |||
|
|||
/** | |||
* Split the collection into several pieces. | |||
* | |||
* @param int $amount |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cs
So it's the same as this? $users->chunk(ceil($users->count() / $n)); ...and this even preserves the keys. |
There is a small difference - there are always exactly N pieces in the result collection [
[1],
[2],
]
[
[1],
[2],
[],
] It's way more simple and clear too |
a52b651
to
5d19771
Compare
Why doesn't it preserve the keys? |
That's been changed in 5.2. The We're moving away from all these Boolean flags. Wherever possible, keys should be preserved. If you don't want that, call |
Oh, as well as chunk I see So it doesn't preserve in 5.1 but gonna preserve in 5.2... Maybe this PR should go to master then or be changed to preserve in 5.1 too? Let's leave it up to Taylor to decide |
Going to hold off on this. |
Use case:
I need to split a collection of arbitary length into 3 columns of relatively equal length.
Notes:
divide
is a better name?