What is the role of the array_search () function with 3rd parameters?

Source: Internet
Author: User
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?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.