Php array lookup functions in_array (), array_search (), and array_key_exists () Use instances

Source: Internet
Author: User
This article mainly introduces the php array lookup function in_array (), array_search (), and array_key_exists () instances. For more information, see

This article mainly introduces the php array lookup function in_array (), array_search (), and array_key_exists () instances. For more information, see

There are many methods for php to search for the existence of a specified value in an array. I remember that I used the foreach loop for a long time ago, next I will mainly share with you the three built-in php array functions to find whether the specified value exists in the array. The three components are in_array (), array_search (), array_key_exists ().

First, we will introduce their respective definitions and functions.

In_array (value, array, type)

This function is used to search for the specified value in the array. type is an optional parameter. If this parameter is set to true, then, check whether the searched data is of the same type as the array value, that is, the constant equals.

Example:

The Code is 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)

This function is used to determine whether a specified key exists in an array. If the key exists, true is returned; otherwise, false is returned.

Example:

The Code is as follows:


$ A = array ("a" => "Dog", "B" => "Cat ");
If (array_key_exists ("a", $ )){
Echo "Key exists! ";
} Else {
Echo "Key does not exist! ";
}
?>

Output:

Key exists!

Array_search (value, array, strict)

The array_search () function is the same as the in_array () function. You can find a key value in the array. If this value is found, the key name matching the element is returned. If not found, false is returned. Note that before PHP 4.2.0, the function returns null instead of false if the function fails. Similarly, if the third parameter strict is specified as true, the key name of the corresponding element is returned only when the data type and value are consistent.

Example:

The Code is as follows:


$ A = array ("a" => "Dog", "B" => "Cat", "c" => 5, "d" => "5 ");
Echo array_search ("Dog", $ );
Echo array_search ("5", $ );
?>

Output:

Ad

After actual performance comparison, when the data volume is small, such as less than 1000, finding which row is used will not become a performance bottleneck. However, when the data volume is large, it is more appropriate to use array_key_exists. According to the test, array_key_exist is dozens or even dozens of times more efficient than in_array.

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.