w3resource

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.

Example:

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

Output:

3

Pictorial Presentation:

php function reference: array_search() function

View the example in the browser

Practice here online :

See also

PHP Function Reference

Previous: array_reverse
Next: array_shift



Follow us on Facebook and Twitter for latest update.