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 a given value in an 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

Serial 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 finds it 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 an 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