array_search in PHP

Syntax of array_search

mixed array_search(mixed value, array array[, bool strict])

Performs a search for a value in an array, as with in_array(). If the value is found, the key of the matching element is returned; NULL is returned if the value is not found. If strict is specified and is true, a matched element is returned only when it is of the same type and value as value

Example of array_search

<?php
$array = array(0 => 'blue', 1 => 'red', 2 => 'green', 3 => 'red');

$key = array_search('green', $array); // $key = 2;
$key = array_search('red', $array);   // $key = 1;
?>

Leave a Comment