PHP Array Lookup function In_array (), Array_search (), array_key_exists () usages

Source: Internet
Author: User

PHP in the array to find the value of the existence of a number of methods there are many, I remember a long time ago I have been silly with the Foreach loop to find, below I mainly share the PHP built-in three array functions to find whether the specified value exists in the array, the three arrays are  in_ Array (), Array_search (), array_key_exists (). First, introduce the respective definition and function In_array (Value,array,type) The function is to search the array of the specified value, type is an optional parameter, if the parameter is set to &NBSP;TRUE&NBSP; Check that the data you are searching for is the same type as the value of the array, that is, constant equals. Example: Copy code code as follows:  <?php$people = array ("Peter",  "Joe",  "Glenn",  "Cleveland"); if (in _array ("Glenn", $people)) {  echo  "Match found";} else{  echo  "Match not found";}? > Output: Match found array_key_exists (Key,array) The function is to determine whether a specified  key exists in an array of arrays, if the  key  exists, Returns &NBSP;TRUE, otherwise returns &NBSP;FALSE. Example: The code is as follows:  <?php$a=array ("a" = "Dog", "b" = "Cat"), if (Array_key_exists ("a", $a)) { echo  "key  exists! ";} else{ echo  "key does not exist!";}? > Output: Key exists!  array_search (value,array,strict) array_search ()   function with  in_array ()  , look for a key value in the array. If the value is found, the key name corresponding to the element is returned. If not found, return to &nbsP;false. Note Before  PHP 4.2.0 , the function returns  null  instead of  false on failure. Similarly, if the third argument  strict  is specified as  true, the key name of the corresponding element is returned only if the data type and value are consistent. Example: The code is as follows:  <?php$a=array ("A" and "dog", "b" = "Cat", "C" =>5, "D" = "5"); Echo array_search ("Dog" , $a); Echo array_search ("5", $a);? > Output: Ad After the actual performance comparison, when the amount of data is not large, such as less than 1000, look for what kind of line, will not become a performance bottleneck. But when the amount of data is relatively large, it is appropriate to use  array_key_exists . Test  array_key_exist  is more than 10 or dozens of times times higher than  in_array  efficiency.   From: http://www.jb51.net/article/49499.htm

PHP Array Lookup function In_array (), Array_search (), array_key_exists () usages

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.