PHP array search

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

PHP: array_search() function

PHP: Searches the array for a given value

The array_search() function is used to search the array against the given value. The function returns the first corresponding key if successful.

Version

(PHP 4 and above)

Syntax

array_search(value_search, array_name, strict) 

Parameters

Name

Description

Required /
Optional

Type

value_search

Value to search

Required

Mixed*

array_name

Input array name.

Required

Array

strict

If TRUE, checks the type of the value. Default value is FALSE

Optional

Boolean

Return value

The key for value_search in array_name.

Value Type : Mixed*.

*Mixed : Mixed indicates that a parameter may accept multiple (but not necessarily all) types.

Pictorial Presentation :

php array array_search() function

Example :

  1. $fruits = array(0 => 'Orange', 1=> 'Apple', 2 => 'Banana',3 => 'Cherry');  

  2. $result = array_search('Cherry', $fruits);  

  3. echo $result;   

  4. ?>  

Output :

3