php in_array(mixed $needle, array $haystack[, bool $strict = FALSE] )
注意: 一、如果$needle 是字符串,则比较是区分大小写的。
二、如果$strict = TRUE 则in_array 会比较$needle 和$haystack 的类型是否一致,不一致,则返回FALSE
1 //例一、in_array 严格类型检查例子 2 <?php 3 $a = array('1.12', 2.16, 3.18); 4 if(in_array('2.16', $a, true)) 5 { 6 echo "'2.16' found with strict check "; 7 } 8 if(in_array(3.18, $a, true)) 9 { 10 echo "3.18 found with strict check "; 11 } 12 ?> 13 //output 3.18 found with struct check 14 //例二、in_array()中用数组作为$needle 15 <?php 16 $a = array(array('p', 'h'), array('j', 'q'), 'o'); 17 if(in_array(array('p', 'h'), $a)) 18 { 19 echo "'ph' was found "; 20 } 21 if(in_array(array('f', 'i'), $a)) 22 { 23 echo "'fi' was found "; 24 } 25 if(in_array('o', $a)) 26 { 27 echo "'fi' was found "; 28 } 29 ?> 30 //output 'ph' was found 31 'o' was found 32 33
in_array 效率问题探讨
分析暂时无时间做
可参考:daiyan_csdn的博客
总结就是:isset 效率 > array_key_exists > in_array
使用isset来判断数据是否在数组中
1 //使用array_flip将数组key与value翻转,通过isset判断key是否存在于数组中 2 public static function inArray($items, $array) 3 { 4 $flip_array = array_flip($array); 5 return isset($fiip_array[$items]); 6 }