Php Array Search Function

What is array_search Function?

Explanation

The "array_search" function searches the array for a given value and returns the corresponding key.

Syntax:


array_search(value,array,strict)

In the above syntax "value" specifies the value to be searched for, "array" specifies the array in which to search, "strict" specifies a strict comparison (===).

Example :


<?php
$b=array("c"=>"Cherry","b"=>"Strawberry");
$a=array("a"=>"5","b"=>5,"c"=>"5");
print_r(array_search("Cherry",$b));
print_r(array_search(5,$a,true);
?>
Result :

c
b

In the above example the array "$b", is searched for and displays the value "cherry", for the array "$a" a strict comparison is done and the result is displayed as "b",since 5 and "5" are not equal when (===) is used.

PHP Topics


Ask Questions

Ask Question