检查是否"运行时间和QUOT;多维数组键存在 [英] Check if a "run-time" multidimensional array key exists
本文介绍了检查是否"运行时间和QUOT;多维数组键存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个多维数组。我需要检查,如果指定的键存在的功能。
让我们来此阵
$配置['库'] ['模板'] ['引擎'] ='设置好的';
当我把它称为一个函数返回true:
checkKey('库','模板','引擎');
//>检查是否使用isset $配置['库'] ['模板'] ['引擎']
请注意,我的阵列不仅是三维的。它应该是能检查即使只有1尺寸:
checkKey('genericSetting');
//>返回false becase的$ C ['genericSetting']没有设置好的
目前我使用的是可怕的评估
code,我想听听建议:)
解决方案
函数checkKey($数组){
的$ args = func_get_args();
为($ i = 1; $ I<计数($参数); $ I ++){
如果(!使用isset($数组[的$ args [$ i]))
返回false;
$阵列=安培; $阵列[的$ args [$ i]];
}
返回true;
}
用法:
checkKey($配置,'库','模板','引擎');
checkKey($配置,genericSetting');
I have a multidimensional array. I need a function that checks if a specified key exists.
Let's take this array
$config['lib']['template']['engine'] = 'setted';
A function should return true when I call it with:
checkKey('lib','template','engine');
//> Checks if isset $config['lib']['template']['engine']
Note that my array isn't only 3 dimensional. It should be able to check even with only 1 dimension:
checkKey('genericSetting');
//> Returns false becase $c['genericSetting'] isn't setted
At the moment I am using an awful eval
code, I would like to hear suggest :)
解决方案
function checkKey($array) {
$args = func_get_args();
for ($i = 1; $i < count($args); $i++) {
if (!isset($array[$args[$i]]))
return false;
$array = &$array[$args[$i]];
}
return true;
}
Usage:
checkKey($config, 'lib', 'template', 'engine');
checkKey($config, 'genericSetting');
这篇关于检查是否&QUOT;运行时间和QUOT;多维数组键存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文