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);

This is an error 500

Value for `_results` contains invalid data `array`


preferences:
164.54 ms | 2170 KiB | 12 Q