繁体   English   中英

PHP array_search

[英]PHP array_search

是否有任何函数可以用作为数组的$ needle完成与array_search等效的功能? 就像,这将是一个理想的解决方案:

$needle = array('first', 'second');
$haystack = array('fifth','second','third', 'first');

// Returns key 1 in haystack
$search = array_search( $needle, $haystack );

如果没有函数,是否还有其他可以接受指针的函数,这些指针可能是我可以用来创建自定义函数的数组?

这可能有助于构建您的功能:

$intersection = array_intersect($needle, $haystack);

if ($intersection) // $haystack has at least one of $needle

if (count($intersection) == count($needle)) // $haystack has every needle

您可以使用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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM