3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php function array_split($input, $size=1) { $result = array(); for ($i = 0; $i < $size; $i++) { $remaining_slots = $size - $i; $remaining_items = count($input); $chunk_size = ceil($remaining_items / $remaining_slots); $chunk = array(); for ($e = 0; $e < $chunk_size; $e++) { $chunk[] = array_shift($input); } $result[] = $chunk; } return $result; } var_dump(array_split(range(0, 4), 4));
Output for 4.3.0 - 5.6.28, hhvm-3.10.0 - 3.12.0, 7.0.0 - 7.1.0
array(4) { [0]=> array(2) { [0]=> int(0) [1]=> int(1) } [1]=> array(1) { [0]=> int(2) } [2]=> array(1) { [0]=> int(3) } [3]=> array(1) { [0]=> int(4) } }