My goal is to write a function which would assign a value to any multidimensional array:
function array_multidim_set($arr, $keys, $value)
Example: print_r(array_multidim_set($arr, ['key1', 'key2', 'key'3'], 'foo')
should create a value as following $arr[key1][key2][key3] = 'foo'
;
For now I'm using this:
function array_multidim_set($arr, $keys, $value){
switch(count($keys)){
case 1:
$arr[$keys[0]] = $value;
break;
case 2:
$arr[$keys[0]][$keys[1]] = $value;
break;
case 3:
$arr[$keys[0]][$keys[1]][$keys[2]] = $value;
break;
...and so on...
}
return $arr;
}
But it's limited to the amount of cases defined. Is there any way to make a universal function for any amount of keys?
Thank you!