[英]in_array() for array of array
Is there a function like in_array()
than can, check conntent inside array of arrays? 是否有像
in_array()
这样的函数可以检查数组内部的内容?
I tried: 我试过了:
$day_events = Array();
array_push($day_events,array('aa','bb','cc'));
array_push($day_events,array('aa','bc','cd'));
array_push($day_events,array('ac','bd','ce'));
echo '<br />';
echo in_array('aa',$day_events); // empty
echo '<br />';
foreach ($day_events as &$value) {
echo in_array('aa',$value); // 11
}
first in_array() which is the kind of function I am looking for (avoiding the loop) gave empty. 第一个in_array()就是我正在寻找的那种函数(避免循环)为空。
Use this function, as in_array
doesn't natively support multi-dimensional arrays: 使用此函数,因为
in_array
本身不支持多维数组:
function in_array_r($needle, $haystack, $strict = false) {
foreach ($haystack as $item) {
if (($strict ? $item === $needle : $item == $needle) || (is_array($item) && in_array_r($needle, $item, $strict))) {
return true;
}
}
return false;
}
In this case you would you use it like this: 在这种情况下,您将像这样使用它:
echo in_array_r('aa', $day_events) ? 'Found' : 'Not found';
It was taken from this answer: https://stackoverflow.com/a/4128377/2612112 . 它是从以下答案中获取的: https : //stackoverflow.com/a/4128377/2612112 。
By the way it is not avoiding first one it is avoiding the last one which has 'ac'. 顺便说一句,它没有避免第一个,而是避免了最后一个有“ ac”的。 So you get true from first two.
所以您从前两个开始就可以了。 Your code works but I am not sure if that is what you want.
您的代码有效,但是我不确定这是否是您想要的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.