3v4l.org

run code in 300+ PHP versions simultaneously
<?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;
Output for 7.1.25 - 7.1.30, 7.2.0 - 7.2.33, 7.3.0 - 7.3.26, 7.4.0 - 7.4.13, 8.0.0 - 8.0.2
Если не завязываться на количестве пробелов: [{"key":"text","prices":["16.11","19.22","21.33"]},{"key":"text","prices":["17.11","23.22","25.33","27.55"]},{"key":"text","prices":["18.11","26.22"]}]