This time to bring you the PHP array lookup function Usage summary, PHP array lookup function Use of the note what, the following is the actual case, take a look.
PHP looks in the array for the existence of the specified value, always using a foreach loop.
There are better ways to use PHP's built-in three array functions to find out if a specified value exists in the array, and the three arrays are In_array (), Array_search (), array_key_exists ().
The definitions and functions of the three PHP array functions are described below.
One, PHP array lookup function 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.
<?php$people = Array ("Peter", "Joe", "Glenn", "Cleveland"), if (In_array ("Glenn", $people)) { echo "Match found";} else{ echo "Match not Found";}? >
Output:
Match found
Second, PHP array lookup function 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.
<?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!
Three, PHP array lookup function 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.
<?php$a=array ("A" and "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.
Believe that you have read the case of this article you have mastered the method, more exciting please pay attention to the PHP Chinese network other related articles!
Recommended reading:
Yii framework to create scheduled tasks through console commands detailed steps
Yii Framework form model submit a form in an array form