Maison >développement back-end >tutoriel php >array_search()函数,第3个参数,有什么作用?
http://docs.php.net/manual/zh/function.array-search.php
mixed array_search ( mixed $needle , array $haystack [, bool $strict = false ] )
如手册中所说一样,当第三个参数为true的时候,则要检查值的类型
如手册中所说一样,当第三个参数为true的时候,则要检查值的类型
给个实例!
<? $x=array("0"=>"Q1","1"=>"Q2","2"=>"Q3","3"=>"q2","4"=>"Q2","5"=>"q2"); $y=array_search("q2",$x); echo $y; echo "\r\n"; $xx=array("0"=>"Q1","1"=>"Q2","2"=>"Q3","3"=>"q2","4"=>"Q2","5"=>"q2"); $yy=array_search("q2",$xx,$strict =false); echo $yy; echo "\r\n"; $xxx=array("0"=>"Q1","1"=>"Q2","2"=>"Q3","3"=>"q2","4"=>"Q2","5"=>"q2"); $yyy=array_search("q2",$xxx,$strict =true); echo $yyy; echo "\r\n";?>
<?php$x=array("0"=>"Q1","1"=>"Q2","2"=>"Q3","3"=>"2","4"=>"Q2","5"=>"q2");$y=array_search("2",$x);echo $y;echo "<hr>";$xx=array("0"=>"Q1","1"=>"Q2","2"=>"Q3","3"=>2,"4"=>"Q2","5"=>"q2");$yy=array_search("2",$xx,$strict =false);echo $yy;echo "<hr>";$xxx=array("0"=>"Q1","1"=>"Q2","2"=>"Q3","3"=>2,"4"=>"Q2","5"=>"q2");$yyy=array_search("2",$xxx,$strict =true);echo $yyy;echo "<hr>";$xxxx=array("0"=>"Q1","1"=>"Q2","2"=>"Q3","3"=>2,"4"=>"Q2","5"=>"q2");$yyyy=array_search(2,$xxx,$strict =true);echo $yyyy;echo "<hr>";
$ar = array(1, 2, 3);var_dump(array_search('2', $ar, true)); //bool(false)var_dump(array_search(2, $ar)); //int(1)
$a = array("a"=>"5","b"=>5);
echo array_search(5,$a);不用判断值的类型 所以字符串"5" 匹配成功
echo '
';
echo array_search(5,$a,true);需要判断值的类型 所以字符串"5" 匹配不成功 数字5匹配成功
<?php$x=array("0"=>"Q1","1"=>"Q2","2"=>"Q3","3"=>"2","4"=>"Q2","5"=>"q2");$y=array_search("2",$x);echo $y;echo "<hr>";$xx=array("0"=>"Q1","1"=>"Q2","2"=>"Q3","3"=>2,"4"=>"Q2","5"=>"q2");$yy=array_search("2",$xx,$strict =false);echo $yy;echo "<hr>";$xxx=array("0"=>"Q1","1"=>"Q2","2"=>"Q3","3"=>2,"4"=>"Q2","5"=>"q2");$yyy=array_search("2",$xxx,$strict =true);echo $yyy;echo "<hr>";$xxxx=array("0"=>"Q1","1"=>"Q2","2"=>"Q3","3"=>2,"4"=>"Q2","5"=>"q2");$yyyy=array_search(2,$xxx,$strict =true);echo $yyyy;echo "<hr>";
$ar = array(1, 2, 3);var_dump(array_search('2', $ar, true)); //bool(false)var_dump(array_search(2, $ar)); //int(1)
手册中有:如果第三个参数 strict 的值为 TRUE 则 in_array() 函数还会检查 needle 的类型是否和 haystack 中的相同。
为什么都不看呢?神仙?
$ar = array(1, 2, 3);var_dump(array_search('2', $ar, true)); //bool(false)var_dump(array_search(2, $ar)); //int(1)