Collection::chunk doesn't allow to change the preserve_keys flag. #49483
Replies: 1 comment 1 reply
-
How has this not been implemented yet? I understand that making a change to keep preserveKeys inline with PHP will break things, but I believe the way @erdemkeren has implemented this allows users to pick wether to use it or not and will not break the framework for others due to it not overriding Laravel's current implementation. I have just hit this issue myself and now I need to use the native php function to get around this quirk, If its too dangerous to do this directly in a function that could be used by millions then create a new function please. Having to go from collection to array is not ideal and is an unnecessary code smell. I suspect this issue hasn't been reviewed due to the fact that its quite a niche use case but still! |
Beta Was this translation helpful? Give feedback.
-
Background:
PHP
array_chunk
accepts 3 params:array $array
,int $length
andbool $preserve_keys = false
.Which means every chunk provided will be reindexed numerically starting from 0.
Laravel Collection::chunk uses a
array_chunk
under the hood. However it passestrue
as the value of$preserve_keys
parameter (remember that PHP's default is false) and it doesn't allow us to pass an optional parameter.Which means even if the chunk is different, the indexes will be preserved.
Problem
Currently, I use the chunk keys as the index of the spreadsheet export keys.
Every chunk should start from the first column of the provided row.
For example:
with
$preserve_keys = true
what I get is:Because I can't pass
$preserve_keys = false
to the method, I need to1- Extend the collection and override the method
2- Use
array_chunk
or 3- Recalculate the index to match the default behaviour.
I could pass ,
false
as the second paremeter instead.So suggestion is:
More
A similar isse was created here: #29685. But it was redirected to the ideas and a PR was requested. Now that the repo is a public archive and we're redirected to here, I thought it is a good idea to discuss it again.
Beta Was this translation helpful? Give feedback.
All reactions