Method One: Adopt In_array (Value,array,type)
Type is optional. If this parameter is set to True, the search data is checked for the same type as the value of the array.
Copy Code code as follows:
$arr = Array (' Can ', ' how ', ' method ', ' Know ', ' no ', ' no ');
In_array (Value,array,type)
$isin = In_array ("How 2", $arr);
if ($isin) {
echo "in====". $isin;
}else{
echo "out====". $isin;
}
Method Two:
The Array_key_exists ' array_key_exists () function determines whether a specified key exists in an array, returns TRUE if the key exists, or returns false. Array_key_exists (Key,array
Method Three:
The Array_search () function, like In_array (), looks for a key value in the array. If the value is found, the key name of the matching element is returned. Returns False if it is not found. Array_search (value,array,strict), when the amount of data is relatively large, with array_key_exists more appropriate, but the memory is larger,
The array structure is: Array (1, 2, 3, ...) and Array (1 => true, 2 => false, ...), memory usage ratio is 1:2.
Specifically related to internal implementation, in PHP, the first and second type of data structure is similar, are associative arrays.
Other supplements:
There are three ways to find whether an element is in an array:
The In_array ' function searches the array for a given value. In_array (value,array,type) type is optional. If this parameter is set to True, the search data is checked for the same type as the value of the array.
The Array_key_exists ' array_key_exists () function determines whether a specified key exists in an array, returns TRUE if the key exists, or returns false.
Array_key_exists (Key,array)
The Array_search ' Array_search () function, like In_array (), looks for a key value in the array. If the value is found, the key name of the matching element is returned. Returns False if it is not found.
Array_search (value,array,strict)
From this point of view, when the amount of data is small, such as less than 1000, to find which one is OK, will not become a bottleneck;
When the amount of data is larger, it is more appropriate to use array_key_exists.
Of course, here array_key_exists occupy a larger memory, after the calculation
The array structure is: Array (1, 2, 3, ...) and Array (1 => true, 2 => false, ...)
Their memory usage ratio is 1:2;
This is related to the internal implementation, which is actually similar to the first and second data structures in PHP, and is an associative array.