Heim  >  Artikel  >  php教程  >  php in

php in

WBOY
WBOYOriginal
2016-06-06 19:42:311954Durchsuche

in_array 是PHP 的 检查数组中是否存在某个值 的函数,里面有三个参数bool in_array ( mixed $needle , array $haystack [, bool $strict = FALSE ] ) mixed:类型 支持做种复合类型,参数传入也是可以是int,str,float,array haystack: 源数组,查找的数组。 str

in_array 是PHP 的检查数组中是否存在某个值 的函数,里面有三个参数 bool in_array ( mixed $needle , array $haystack [, bool $strict = FALSE ] ) 

mixed:类型 支持做种复合类型,参数传入也是可以是int,str,float,array 

haystack:  源数组,查找的数组。

strict : 参数接受两个 true 和false 两个参数.该参数主要检查 $needle  和$haystack  中的value 的类型是否一致。

有趣的问题:

<span>1</span> <span>var_dump</span>(<span>in_array</span>('b', <span>array</span>('a'=><span>true</span><span>)));
</span><span>2</span> 返回值:<span>true</span>
<span>3</span> 
<span>4</span> <span>var_dump</span>(<span>in_array</span>('01',<span>array</span>('1'<span>)));
</span><span>5</span> 返回值也是:<span>true</span>

其实上面的问题不难接受:

第一种、var_dump(in_array('b', array('a'=>true)))  这个比较是比较 'b'==true 这样的 类型比较 b 是变量或者一个字符串string  和bool 类型比较.结果是true

具体的可以看官方的:http://php.net/manual/zh/types.comparisons.php  但是如果 var_dump('b'===true); 结果可能就不一样了返回值:false 就是类型比较的问题

第二种、其实也一样都是在类型判断的问题。大家可以在开发环境中试试。

var_dump('01'==1); 返回值:true
var_dump('01'===1);返回值:false 

大家有心情可以看下in_array 源码.https://github.com/php/php-src/blob/master/ext/standard/array.c

源码中有php_search_array 的源码,当使用非严格模式时,调用 fast_equal_check_function 函数。

所以如果在使用in_array 查找的数据,如果对类型要求严格,请这样使用:var_dump(in_array('01',array('1'),true)); strict :传递true .

 

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn