3v4l.org

run code in 300+ PHP versions simultaneously
<?php class NotJsonParser { public function convertPartsToObject(array $lines): array { $result = []; $result[]['items'] = &$items; foreach ($lines as $line) { if ($line[0] === '-') { // or however you want to identify the separator unset($items); $result[]['items'] = &$items; } else { $items[] = array_combine(['label', 'value', 'color'], explode('|', $line, 3)); } } return $result; } } $string = <<<STRING Label 1|80|default Label 2|100|default --- Frontend|80|red Backend|20|red --- Another Item 1|20|blue Another Item 2|20|blue - And another Item|20|blue STRING; $object = new NotJsonParser(); var_export($object->convertPartsToObject(explode(PHP_EOL, $string)));

preferences:
29.48 ms | 406 KiB | 5 Q