3v4l.org

run code in 300+ PHP versions simultaneously
<?php $myArray = ['a','b','c','title1','d','e','f','title2','g','h','title3','i','title4','j','k']; $batch = 0; // default value foreach ($myArray as $v) { if (strpos($v, 'title') !== 0) { // value is NOT a "title#"" string $result[$batch][] = $v; // save with current batch value as 1st level key } else { $batch = $v; // update batch value with "title#" string } } var_export($result);

preferences:
103.88 ms | 1543 KiB | 5 Q