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