One of the main operations when working with arrays is the search for a specific value. The PHP function array_search () is for this purpose intended. It is able to process both one-dimensional and associative collections, returning the key of the desired value if it is found in the array.
Syntax
The formalized description of the array_search () function in PHP is as follows:
mixed array_search (mixed value, array $collection [, bool strict])
Input parameters:
- $ collection - the array in which the search will be performed;
- value - the desired value of any type;
- strict is an optional boolean flag that sets a strict type-based comparison mechanism.
Work mechanism
The PHP function array_search () alternately compares value with all the values ββin the collection array. By default, comparison is performed without regard to operand types. You can change this setting by setting the strict flag to TRUE. String comparisons are case sensitive.
If a match is found, the key corresponding to the found element is returned, and the function stops. Therefore, it cannot be used to detect multiple occurrences of the desired value in an array.
If no matches are found, the function will return a Boolean value of FALSE.
The returned result should be checked using the strict equality operator (===). This is important because the function can return a value that is cast to FALSE, for example, 0 or an empty string.
Examples of using
Example 1. When passing a multidimensional array to the PHP function array_search (), the result of the operation will be the key of the element being searched.
<?php $array = array( "season1" => "winter", "season2" => "spring", "season3" => "summer", "season4" => "autumn" ); $result1 = array_search("winter", $array); $result2 = array_search("summer", $array); $result3 = array_search("april", $array); ?>
In this example, the variable $ result1 will get the value "season1", $ result2 will be equal to "season3", and $ result3 will be set to the Boolean value FALSE, since the string "april" does not occur in the original array.
Example 2. The PHP function array_search () can also process a one-dimensional array, considering its keys to be the next numerical indices.
<?php $array = array("", "", "", "", "", "", ""); $result = array_search("", $array); ?>
The $ result variable will be set to 1, according to the index of the hunter element in the $ array.
Example 3. A possible error in the analysis of the result.
<?php $presidents = array( 0 => "Washington", 1 => "Adams", 2 => "Jefferson", 3 => "Madison", 4 => "Monroe" ); $result = array_search("Washington", $presidents); if (!$result) { echo "G. Washington was not the first president of the USA"; } ?>
So, without checking the result by strict equality, you can get an unexpected message that George Washington was not the first president of the United States.
Example 4. Only the key of the first match found is returned.
<?php $song = ["jingle", "bells", "jingle", "bells", "jingle", "all", "the", "way]; $result = array_search("jingle", $song); echo $result; ?>
Despite the fact that the desired value occurs three times in the array, the function will return only the first result found - 0. To search for multiple matches, it is recommended to use the PHP function array_keys ().