3v4l.org

run code in 300+ PHP versions simultaneously
<?php $input = "objectId | 19 location.street | Rue des clochets translations.fr.idTranslation | 4"; $output = []; $lines = explode("\n", $input); foreach ($lines as $line) { $cols = explode("|", $line); $key = trim($cols[0]); $value = trim($cols[1]); $indices = explode('.', $key); $first = array_shift($indices); if (!isset($output[$first])) $output[$first] = []; $target = &$output[$first]; foreach ($indices as $index) { if (!isset($target[$index])) { $target[$index] = []; $target = &$target[$index]; } } $target = $value; } var_dump($output);
Output for 5.6.0 - 5.6.40, 7.0.0 - 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.27, 8.4.1 - 8.4.14
array(3) { ["objectId"]=> string(2) "19" ["location"]=> array(1) { ["street"]=> string(16) "Rue des clochets" } ["translations"]=> array(1) { ["fr"]=> array(1) { ["idTranslation"]=> &string(1) "4" } } }

preferences:
150.92 ms | 409 KiB | 5 Q