检查如果In_Array(递归关联数组) [英] Check If In_Array (in Recursive Associative Array)
本文介绍了检查如果In_Array(递归关联数组)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有类似这样的数组:
阵列(2){
[0] =>
阵列(2){
[code] =>
字符串(2)EN
[名称] =>
字符串(7)英语
}
[1] =>
阵列(2){
[code] =>
字符串(2)BG
[名称] =>
串(9)保加利亚
}
}
我如何检查如果字符串保加利亚是上面阵列的一部分,或者如果郎code'恩'是阵列的一部分?这将是巨大的,如果我没有在整个阵列使用的foreach循环和字符串每个元素['code']或元素['名']进行比较。
解决方案
// $类型可以是code或名称
功能check_in_array($改编,价值$,$ TYPE =code){
返回计数(array_filter($ ARR,功能($ VAR)使用($型,$值){
回报是$ var [$类型] === $价值;
!}))== 0;
}
I have an array similar to this:
array(2) {
[0]=>
array(2) {
["code"]=>
string(2) "en"
["name"]=>
string(7) "English"
}
[1]=>
array(2) {
["code"]=>
string(2) "bg"
["name"]=>
string(9) "Bulgarian"
}
}
How do I check if the string Bulgarian is part of the above array, or alternatively if the lang code 'en' is part of the array? It would be great if I didn't have to use foreach to loop through the entire array and compare the string with each element['code'] or element['name'].
解决方案
// $type could be code or name
function check_in_array($arr, $value, $type = "code") {
return count(array_filter($arr, function($var) use ($type, $value) {
return $var[$type] === $value;
})) !== 0;
}
这篇关于检查如果In_Array(递归关联数组)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文