There are many ways to find the values specified in the array in PHP, let me introduce the array lookup function in PHP in_array (), array_key_exists (), Array_search () usage
To find out if an element is in an array, there are three ways to do it altogether:
The In_array ' function searches the array for the given value. In_array (value,array,type) type is optional. If set to true, checks whether the searched data is the same as the type of the array's value.
The Array_key_exists ' array_key_exists () function determines whether the specified key exists in an array, returns TRUE if the key exists, or false otherwise. Array_key_exists (Key,array)
Example #1 array_key_exists () example
| The code is as follows |
Copy Code |
|
$search _array = Array (' First ' = 1, ' second ' + 4);
if (array_key_exists (' first ', $search _array)) {
echo "The ' first ' element is in the array";
}
?> |
Example #2 array_key_exists () vs. Isset ()
Isset () does not return TRUE for values that are NULL in the array, and array_key_exists ().
| The code is as follows |
Copy Code |
|
$search _array = Array (' first ' = null, ' second ' = 4);
Returns false
Isset ($search _array[' first ');
Returns True
Array_key_exists (' first ', $search _array);
?> |
Mixed Array_search (mixed $needle, array $haystack [, BOOL $strict])
The first parameter is the value that needs to be looked up, the second parameter is an array, and the last parameter is whether to check whether the data type is the same at the time of lookup.
| The code is as follows |
Copy Code |
|
$a =array ("a" = "Dog", "b" = "Cat", "C" and "Horse");
Echo Array_search ("Dog", $a);
?>
$a =array ("a" = "5", "B" =>5, "c" = "5");
Echo Array_search (5, $a, true);
?>
The code above will output the following result:
B |
From this point of view, when the amount of data is not large, such as less than 1000, look for what kind of line, will not become a bottleneck;
When the amount of data is relatively large, it is more appropriate to use array_key_exists.
Of course, the array_key_exists occupy a large amount of memory here, after measuring
The array structure is: Array (1, 2, 3, ...) and Array (1 = true, 2 = False, ...)
Their memory usage ratio is 1:2;
Note: Array_key_exist is more than 10 or dozens of times times more efficient than In_array.
A simple algorithm
Suppose the array has 1000 elements, the key value is a unordered positive integer less than 1000000, and is not contiguous, as follows
$arr = Array (1 = ' sadas ', + = ' aasd ', 5002 = ' fghfg ', 190023 = ' rty ', 248 = ' kj ', "sddd",.. .);
Now to get the element with the value of the key in the array $arr greater than 500 less than 600, is there a more efficient algorithm without using foreach to loop through it all over again?
Answer
| The code is as follows |
Copy Code |
|
$result = Array ();
for ($i = 501; $i < $i + +) {
if (!isset ($arr [$i])) continue;
$result [] = $arr [$i];
}
|
http://www.bkjia.com/PHPjc/628954.html www.bkjia.com true http://www.bkjia.com/PHPjc/628954.html techarticle There are many ways to find the values specified in the array in PHP, let me introduce the array lookup function in PHP in_array (), array_key_exists (), Array_search () usage to find whether an element is in an array ...