- array_combine: documentation ( source)
- var_export: documentation ( source)
- explode: documentation ( source)
<?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)));