Skip to content
PHP Logo

How can one efficiently split a list into multiple arrays in PHP?

To efficiently split a list into multiple arrays in PHP, you can use the array_chunk() function. This function splits an array into chunks of a specified size and returns a multidimensional array containing these chunks. By specifying the size of each chunk, you can easily split a list into multiple arrays.

php
$list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
$chunkedArrays = array_chunk($list, 3);

print_r($chunkedArrays);