w3resource logo


PHP Tutorial

PHP: array_search() function

Secondary Nav

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 :

<?php
$fruits = array(0 => 'Orange', 1=> 'Apple', 2 => 'Banana',3 => 'Cherry');
$result = array_search('Cherry', $fruits);
echo $result; 
?>

Output :

3

View the example in the browser

Practice here online :


See also

PHP Function Reference



Join our Question Answer community to learn and share your programming knowledge.