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)));
Output for git.master, git.master_jit, rfc.property-hooks
array ( 0 => array ( 'items' => array ( 0 => array ( 'label' => 'Label 1', 'value' => '80', 'color' => 'default', ), 1 => array ( 'label' => 'Label 2', 'value' => '100', 'color' => 'default', ), ), ), 1 => array ( 'items' => array ( 0 => array ( 'label' => 'Frontend', 'value' => '80', 'color' => 'red', ), 1 => array ( 'label' => 'Backend', 'value' => '20', 'color' => 'red', ), ), ), 2 => array ( 'items' => array ( 0 => array ( 'label' => 'Another Item 1', 'value' => '20', 'color' => 'blue', ), 1 => array ( 'label' => 'Another Item 2', 'value' => '20', 'color' => 'blue', ), ), ), 3 => array ( 'items' => array ( 0 => array ( 'label' => 'And another Item', 'value' => '20', 'color' => 'blue', ), ), ), )

This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.

Active branches

Archived branches

Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page


preferences:
141.21 ms | 409 KiB | 5 Q