In PHP you can divide an array into an unknown number of chunks by specifying the number of elements in each chunk. To do this you would use the array_chunk
function. But what happens when you don’t know the number of elements and just want to split the array in an X number of elements? Unfortunately, we don’t have such a function in PHP but of course, we can create our own function.
In a quick google search, you can find hundreds of solutions, but from my experience, I was unable to find something simple to do the job. Most of them seem to be very complex and vulnerable to bugs.
OK. No more text, here is the function:
function splitMyArray(array $input_array, int $size, $preserve_keys = null): array { $nr = (int)ceil(count($input_array) / $size); if ($nr > 0) { return array_chunk($input_array, $nr, $preserve_keys); } return $input_array; }
Usage:
Suppose that you have this array:
$myArray = [
'a',
'b',
'c',
'd',
'e',
'f',
'g',
'h',
]
And you want to divide it into 3 parts. Here is how you do it:
splitMyArray($myArray, 3)
The result will be:
[ 0 => [ 0 => 'a', 1 => 'b', 2 => 'c', ], 1 => [ 0 => 'd', 1 => 'e', 2 => 'f', ], 2 => [ 0 => 'g', 1 => 'h', ], ]
Pretty nice, don’t you think? Easy and simple. As Leonardo da Vinci said: “Simplicity is the ultimate sophistication.”
Note: Make sure that you have enough elements in the array. At least 2 items for each chunk.
Force chunks size
If you have a few elements only and want more chunks, with the previous function will not be possible to have it. Instead, we have another function that will force the chunks to be, even if they are empty at the end:
function splitMyArray(array $input_array, int $num_chunks, $preserve_keys = null): array
{
$chunk_size = (int)ceil(count($input_array) / $num_chunks);
return array_chunk($input_array, $chunk_size, $preserve_keys);
}
So, if you pass an input array with 3 elements and you want to split it into 5 chunks, the splitMyArray()
function will return an array with 5 elements, where the first 3 elements are the original elements of the input array, and the remaining 2 elements are empty arrays.
Here’s an example:
$input_array = ['a', 'b', 'c'];
$num_chunks = 5;
$preserve_keys = true;
$result = splitMyArray($input_array, $num_chunks, $preserve_keys);
print_r($result);
This would output the following:
Array
(
[0] => Array
(
[0] => a
)
[1] => Array
(
[1] => b
)
[2] => Array
(
[2] => c
)
[3] => Array
(
)
[4] => Array
(
)
)
As you can see, the function returns an array of 5 elements, with the first 3 elements containing the original elements of the input array, and the remaining 2 elements are empty arrays.
This behavior is due to the fact that the input array has fewer elements than the desired number of chunks, so the remaining chunks are simply empty arrays. If you want to distribute the elements of the input array evenly across all chunks, you would need to repeat some of the elements in the resulting chunks, which is outside the scope of the current implementation of the splitMyArray() function.
Saved as a favorite!, I love your web site!
If I want to split the same example into 6 chunks, this code does not work. Output is split into 4 chunks instead of 6
Yep! Unfortunately, it tries to have at least 2 items per chunk.