SoFunction
Updated on 2025-04-08

PHP Search for a given simple instance in an array array array_search function

array_search()

The PHP array_search() function is used to search for a given value in an array, and returns the corresponding key name if successful, otherwise returns FALSE.

grammar:

mixed array_search( mixed needle, array array [, bool strict] ) parameter description:

parameter illustrate
needle The value that needs to be searched in the array, if it is a string, it is case sensitive
array Arrays to be retrieved
strict Optional, if set to TRUE, the value types in the needle and array are also checked

Since the starting index number of the index array may be 0 , the function may also return non-boolean values ​​equivalent to FALSE, such as 0 or "", so the === operator needs to be used to strictly check the value returned by the function.

example:

<?php
$arr_a = array(0 => "a", 1 => "b", 2 => "c");
$key = array_search("a", $arr_a);
if( $key !== FALSE ){
  echo "Key name: $key";
} else {
  echo 'No matching result';
}
?>

The example output results are as follows:

The key name is: 0 If the target appears more than once in the array, the first matching key is returned. To return keys for all matching values, use the array_keys() function.

The above PHP search for a given simple example in the array. The array_search function is all the content I share with you. I hope you can give you a reference and I hope you can support me more.