Possible Duplicate:
Check if a “run-time” multidimensional array key exists
Hi,
I have a multidimensional array. I need a function that checks if a specified key exists and if not set the value.
Let's take this array
$config['lib']['template']['engine'] = false;
A function should not update the value to true when i call with:
checkAndSetKey('lib template engine',true);
//> Checks if isset $config['lib']['template']['engine'] and if not isset $config['lib']['template']['engine'] = true;
Note that my array isn't only 3 dimensional. It should be able to check and set even with only 1 dimension:
checkAndSetKey('genericSetting',true);
//> In this considering there isn't any $c['genericSetting'] the function set the key to true;
At the moment I am using an awful eval code, I would like to hear suggest :)
To dynamically check if the key exists it could be used this code:
$array = $config;
$keys=explode(' ',$argument1);
foreach($keys as $v) {
if (!array_key_exists($v,$array)) {
//> [todo!] the current key doens't exist now we should set the value
}
$array = &$array[$v];
}