http://docs.php.net/manual/zh/function.array-search.php
mixed array_search ( mixed $needle , array $haystack [, bool $strict = false ] )
マニュアルにも記載されている通り、第3引数がtrueの場合は値の型をチェックする必要があります
マニュアルにも記載されている通り、第3引数が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 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)
3 番目のパラメータである in_array() 関数もこのように動作しますか?
見てみませんか?不死身?
$ar = array(1, 2, 3);var_dump(array_search('2', $ar, true)); //bool(false)var_dump(array_search(2, $ar)); //int(1)