PHP Tutorial - PHP array_search() Function






Definition

The array_search() function search an array for a value and returns the key.

Syntax

PHP array_search() Function has the following syntax.

array_search(value,array,strict)

Parameter

ParameterIs RequiredDescription
valueRequired.Value to search for
arrayRequired.Array to search in
strictOptional.If go strict

Possible values:

  • true - search for identical elements in the array. the number 3 is not the same as "3"
  • false - Default.search for value in the array. the number 3 is the same as "3"




Example 1

Search an array for an element


<?php
    $a=array("a"=>"Java","b"=>"PHP","c"=>"java2s.com");
    echo array_search("PHP",$a);
?>

The code above generates the following result.

Example 2

Search an array for the value 5 and return its key. Pay more attention to the "".


<?php
    $a=array("a"=>"5","b"=>5,"c"=>"5");
    echo array_search(5,$a,true);
?>

The code above generates the following result.