3v4l.org

run code in 300+ PHP versions simultaneously
<?php $array = []; $keys = '[a][b][c]'; $value = 'HELLO WORLD'; function setArrayValueFromKeys(&$array, $keys, $value) { $keys = explode('][', trim($keys, '[]')); $reference = &$array; foreach ($keys as $key) { if (!array_key_exists($key, $reference)) { $reference[$key] = []; } $reference = &$reference[$key]; } $reference = $value; } setArrayValueFromKeys($array, $keys, $value); var_dump($array);

preferences:
24.18 ms | 406 KiB | 5 Q