php in_array(mixed $needle, array $haystack[, bool $strict = FALSE] )
注意: 一、如果$needle 是字符串,则比较是区分大小写的。
二、如果$strict = TRUE 则in_array 会比较$needle 和$haystack 的类型是否一致,不一致,则返回FALSE
//例一、in_array 严格类型检查例子
<?php
$a = array('1.12', 2.16, 3.18);
if(in_array('2.16', $a, true))
{
echo "'2.16' found with strict check\n";
}
if(in_array(3.18, $a, true))
{
echo "3.18 found with strict check\n";
}
?>
//output 3.18 found with struct check
//例二、in_array()中用数组作为$needle
<?php
$a = array(array('p', 'h'), array('j', 'q'), 'o');
if(in_array(array('p', 'h'), $a))
{
echo "'ph' was found\n";
}
if(in_array(array('f', 'i'), $a))
{
echo "'fi' was found\n";
}
if(in_array('o', $a))
{
echo "'fi' was found\n";
}
?>
//output 'ph' was found
'o' was found
in_array 效率问题探讨
分析暂时无时间做
可参考:daiyan_csdn的博客
总结就是:isset 效率 > array_key_exists > in_array
使用isset来判断数据是否在数组中
//使用array_flip将数组key与value翻转,通过isset判断key是否存在于数组中
public static function inArray($items, $array)
{
$flip_array = array_flip($array);
return isset($fiip_array[$items]);
}