[英]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.