PHP Array Lookup function In_array (), Array_search (), array_key_exists () Use instance _php Tutorial

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 respectively.

In_array (Value,array,type)

The function is to search the array for the specified value, type is an optional parameter, and if set to true, checks whether the searched data is the same type as the value of the array, that is, constant equals.

Example:
Copy the Code code as follows:
$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 the specified key exists in an array of arrays, returns True if the key exists, or false otherwise.

Example:
Copy the Code code as follows:
$a =array ("a" and "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)

The Array_search () function, like In_array (), looks for a key value in the array. If the value is found, the key name corresponding to the element is returned. Returns False if it is not found. Note Before PHP 4.2.0, the function returns null instead of false on failure. Similarly, if the third parameter, strict, is specified as true, the key name of the corresponding element is returned only if the data type and value are consistent.

Example:

Copy the Code code as follows:
$a =array ("a" = "Dog", "b" = "Cat", "C" =>5, "D" and "5");
Echo Array_search ("Dog", $a);
Echo Array_search ("5", $a);
?>

Output:

Ad

After the actual performance comparison, when the amount of data is small, 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 more appropriate to use array_key_exists. The test array_key_exist was more than 10 or dozens of times times more efficient than In_array.

http://www.bkjia.com/PHPjc/762611.html www.bkjia.com true http://www.bkjia.com/PHPjc/762611.html techarticle 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 ...

  • Related Article

    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.