What is the role of the array_search () function with 3rd parameters? Http://docs.php.net/manual/zh/function.array-search.php
mixed array_search ( mixed $needle , array $haystack [, bool $strict = false ] )
Strict
If the optional third parameter strict is TRUE, array_search () checks for identical elements in haystack. This means that the needle type in haystack is also checked, and the object must be the same instance.
Problem:
This function has 3rd parameters: $ strict = false. what is the function?
To an instance.
Reply to discussion (solution)
As described in the manual, when the third parameter is true, you must check the type of the value.
As described in the manual, when the third parameter is true, you must check the type of the value.
To an instance!
"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";?>
The above code adds the parameter:, $ strict = true or, $ strict = false
No difference?
"Q1","1"=>"Q2","2"=>"Q3","3"=>"2","4"=>"Q2","5"=>"q2");$y=array_search("2",$x);echo $y;echo "";$xx=array("0"=>"Q1","1"=>"Q2","2"=>"Q3","3"=>2,"4"=>"Q2","5"=>"q2");$yy=array_search("2",$xx,$strict =false);echo $yy;echo "";$xxx=array("0"=>"Q1","1"=>"Q2","2"=>"Q3","3"=>2,"4"=>"Q2","5"=>"q2");$yyy=array_search("2",$xxx,$strict =true);echo $yyy;echo "";$xxxx=array("0"=>"Q1","1"=>"Q2","2"=>"Q3","3"=>2,"4"=>"Q2","5"=>"q2");$yyyy=array_search(2,$xxx,$strict =true);echo $yyyy;echo "";
I changed q2 to 2 .. Then, use false and true to output the data respectively .. The third is not output .. If it is true, the type and value must be the same. But the third is to check "2 ".. 2 in the array ,. If the data type is different, the output is not displayed. if the data is printed, the value is false .. If it is set to false, the type is ignored. Therefore, two outputs are available.
$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); no need to judge the value type, so the string "5" is matched successfully.
Echo'
';
Echo array_search (5, $ a, true); the value type must be determined. Therefore, the string "5" does not match successfully. the number 5 is matched successfully.
"Q1","1"=>"Q2","2"=>"Q3","3"=>"2","4"=>"Q2","5"=>"q2");$y=array_search("2",$x);echo $y;echo "";$xx=array("0"=>"Q1","1"=>"Q2","2"=>"Q3","3"=>2,"4"=>"Q2","5"=>"q2");$yy=array_search("2",$xx,$strict =false);echo $yy;echo "";$xxx=array("0"=>"Q1","1"=>"Q2","2"=>"Q3","3"=>2,"4"=>"Q2","5"=>"q2");$yyy=array_search("2",$xxx,$strict =true);echo $yyy;echo "";$xxxx=array("0"=>"Q1","1"=>"Q2","2"=>"Q3","3"=>2,"4"=>"Q2","5"=>"q2");$yyyy=array_search(2,$xxx,$strict =true);echo $yyyy;echo "";
I changed q2 to 2 .. Then, use false and true to output the data respectively .. The third is not output .. If it is true, the type and value must be the same. But the third is to check "2 ".. 2 in the array ,. If the data type is different, the output is not displayed. if the data is printed, the value is false .. If it is set to false, the type is ignored. Therefore, two outputs are available.
Normal!
$ar = array(1, 2, 3);var_dump(array_search('2', $ar, true)); //bool(false)var_dump(array_search(2, $ar)); //int(1)
The in_array () function has 3rd parameters. Is that true?
In the manual, if the value of the third strict parameter is TRUE, the in_array () function also checks whether the needle type is the same as that in haystack.
Why not? Fairy?
$ar = array(1, 2, 3);var_dump(array_search('2', $ar, true)); //bool(false)var_dump(array_search(2, $ar)); //int(1)
The in_array () function has 3rd parameters. Is that true?