PHP array_search() Usage Guide
The array_search() function is used to search for a specified value in an array and return the corresponding key name.
The syntax is:
array_search( $value, $array, $strict )
Parameter Description:
- $value: the value to search for.
- $array: array to be searched.
- $strict (optional): Whether to use strict mode comparison, default is false. If set to true, type checking will be performed during the search.
Return value:
- Return the corresponding key name if the specified value is found, and return false if not found.
Example Usage:
$fruits = array("apple", "banana", "orange", "pear");
$key = array_search("orange", $fruits);
echo $key; // 输出2
$numbers = array(1, 2, 3, 4, 5);
$key = array_search(4, $numbers, true);
echo $key; // 输出3
$key = array_search("5", $numbers);
echo $key; // 输出false
In the example above, the array_search() function searches for a specified value in the $fruits array and $numbers array, and returns the corresponding key. If the value is not found, it returns false. In the third example, because of the default non-strict mode, the comparison between the string “5” and the integer 5 results in true, and the corresponding key is found.