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

Source: Internet
Author: User

There are many ways in which PHP can find a specified value in an array, remember a long time ago I have been silly with the Foreach loop to find, I mainly share the following three array functions in PHP to find whether the specified value exists in the array, the three arrays are In_array (), Array_search (), array_key_exists ().

First of all, introduce their respective definitions and functions

In_array (Value,array,type)

The function is to search for the specified value value in array arrays, type is an optional argument, and if this argument is true, check that the search data 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, and returns True if the key exists, or false.

Example:

Copy Code code 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)

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 that 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 both the data type and the value are consistent.

Example:

Copy Code code as follows:

<?php
$a =array ("a" => "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, to find which one is OK, will not become a bottleneck in performance. But when the amount of data is larger, it is more appropriate to use array_key_exists. According to Test array_key_exist is more than 10 or even dozens of times times more efficient than In_array.

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.