[英]PHP array_search
Is there any function which will accomplish the equivalent of array_search with a $needle that is an array? 是否有任何函数可以用作为数组的$ needle完成与array_search等效的功能? Like, this would be an ideal solution: 就像,这将是一个理想的解决方案:
$needle = array('first', 'second');
$haystack = array('fifth','second','third', 'first');
// Returns key 1 in haystack
$search = array_search( $needle, $haystack );
If no function, any other functions that accept needles which may be arrays that I can use to create a custom function? 如果没有函数,是否还有其他可以接受指针的函数,这些指针可能是我可以用来创建自定义函数的数组?
This might help build your function: 这可能有助于构建您的功能:
$intersection = array_intersect($needle, $haystack);
if ($intersection) // $haystack has at least one of $needle
if (count($intersection) == count($needle)) // $haystack has every needle
You can use array_intersect() : http://php.net/manual/en/function.array-intersect.php 您可以使用array_intersect(): http : //php.net/manual/en/function.array-intersect.php
if (empty(array_intersect($needle, $haystack)) {
//nothing from needle exists in haystack
}
$needle = array('first', 'second');
$haystack = array('fifth','second','third', 'first');
// Returns key 1 in haystack
function array_needle_array_search($needle, $haystack)
{
$results = array();
foreach($needle as $n)
{
$p = array_search($n, $haystack);
if($p)
$results[] = $p;
}
return ($results)?$results:false;
}
print_r(array_needle_array_search($needle, $haystack));
$needle = array('first', 'second');
$haystack = array('fifth','second','third', 'first');
if(in_array($needle, $haystack)) {
echo "found";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.