We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
this function is like array_chunk, but instead of making it chunk size, it will use partition.
The difference with array_chunk is that it chunks the elements in a multidimensional array up to size.
function array_partition($array=[], $size=1, $preserve_keys=false): array { $array = (array) $array; $size = (int) $size; if($size < 1){ throw new Exception('Size must be greater than 0'); } $result = []; $partition = []; $counter = 0; $count = count($array); $amount = (int) ceil($count / $size); if($preserve_keys === false){ foreach ($array as $value) { $partition[] = $value; $counter++; if ($counter === $amount) { $result[] = $partition; $partition = []; $counter = 0; } } } else { foreach ($array as $key => $value) { $partition[$key] = $value; $counter++; if ($counter === $amount) { $result[] = $partition; $partition = []; $counter = 0; } } } if(!empty($partition)){ $result[] = $partition; } return $result; }
$partition = array_partition($array, 2);
results in
$partition = [ 0 => [...], 1 => [...] ]
The text was updated successfully, but these errors were encountered:
found my oneliner:
$chunks = array_chunk($list, ceil(count($list) / $threads));
so function could be:
/** * @throws Exception */ function array_partition($array=[], $size=1, $preserve_keys=false): array { $array = (array) $array; $size = (int) $size; if($size < 1){ throw new Exception('Size must be greater than 0'); } return array_chunk($array, ceil(count($array) / $size), $preserve_keys); }
Sorry, something went wrong.
No branches or pull requests
Description
this function is like array_chunk, but instead of making it chunk size, it will use partition.
The difference with array_chunk is that it chunks the elements in a multidimensional array up to size.
results in
The text was updated successfully, but these errors were encountered: