How to print array elements in groups of 2 with PHP

1 Answer

0 votes
$l = [1, 2, 3, 4, 5, 6, 7, 8];

foreach(array_chunk($l, 2) as $x) {
    echo $x[0], ' ', $x[1], PHP_EOL;
}


/*
run:

1 2
3 4
5 6
7 8

*/

 



answered Jun 21, 2025 by avibootz
...