3v4l.org

run code in 300+ PHP versions simultaneously
<?php $input = [1, 1, 1, 2, 2, 2, 1, 1, 2, 2, 1]; $output = []; $idx = -1; $prev = null; foreach($input as $val) { if($prev !== $val) { ++$idx; } $output[$idx][] = $val; $prev = $val; } print_r($output);
Output for 7.3.0 - 7.3.33, 7.4.0 - 7.4.29, 8.0.0 - 8.0.19, 8.1.0 - 8.1.6
Array ( [0] => Array ( [0] => 1 [1] => 1 [2] => 1 ) [1] => Array ( [0] => 2 [1] => 2 [2] => 2 ) [2] => Array ( [0] => 1 [1] => 1 ) [3] => Array ( [0] => 2 [1] => 2 ) [4] => Array ( [0] => 1 ) )

preferences:
51.81 ms | 410 KiB | 5 Q