English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

PHP Basic Tutorial

PHP Advanced Tutorial

PHP & MySQL

PHP Reference Manual

PHP array_search() function usage and example

PHP Array Function Manual

 The PHP array_search() function searches for the given value in the array and returns the first matching key name if successful.

Syntax

array_search($value, $array [,$strict]);

Definition and Usage

The array_search() function searches for a value in an array and returns the key.

Parameter

NumberParameters and Description
1

value (required)

It specifies the value to be searched.

2

array (required)

It specifies an array.

3

strict (optional)

If set to true, array_search() will also check the search type in the array.

Return value

If it is found in the array, it returns the key, otherwise it returns FALSE.

If the value appears more than once in the array, it returns the first matching key. To return all matching keys, use array_keys() with the optional parameter search_value instead.

Online example

Search for a given value in an array

<?php
   $input = array("a" => "banana", "b" => "apple", "c" => "Mango");
   
   print_r(array_search("apple", $input));
?>
Test and see‹/›

Output result:

b

 PHP Array Function Manual