- var_dump: documentation ( source)
- explode: documentation ( source)
- trim: documentation ( source)
<?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);