验证PHP数组的数组键 [英] Validate array keys of a php array
本文介绍了验证PHP数组的数组键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两个数组: $值
和 $ availableKeys
。我想扔,如果 $值
包含不允许键例外。目前,我正在这code。
的foreach($值$关键=> $值){
如果(!in_array($键,$ availableKeys)){
抛出新的RuntimeException(
不允许钥匙
);
}
}
存在更好的方法来验证一个数组?问题验证PHP数组键方式>值没有回应我的问题。
解决方案
如果(array_diff_key($值,array_flip($ availableKeys))){
抛出新的RuntimeException(..);
}
I have two arrays: $values
and $availableKeys
. I want to throw an exception if $values
contains not allowed keys. At the moment I am running this code.
foreach ($values as $key => $value) {
if (!in_array($key, $availableKeys)) {
throw new RuntimeException(
'Not allowed key'
);
}
}
Exists a better way to validate an array? The question Validate PHP Array Key>Value is not responding to my question.
解决方案
if (array_diff_key($values, array_flip($availableKeys))) {
throw new RuntimeException(..);
}
这篇关于验证PHP数组的数组键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文