- array_shift: documentation ( source)
- trim: documentation ( source)
- preg_split: documentation ( source)
- json_encode: documentation ( source)
<?php
$str = <<<'EOF'
text 16.11 19.22 21.33
text 17.11 23.22 25.33 27.55
text 18.11 26.22
EOF;
echo 'Если не завязываться на количестве пробелов: ', PHP_EOL;
$res = [];
$lines = preg_split('#[\n\r]+#', trim($str));
foreach ($lines as $line) {
$prices = preg_split('#\s+#', $line);
$key = array_shift($prices);
$res[] = ['key' => $key, 'prices' => $prices];
}
echo json_encode($res), PHP_EOL;