how to find an element by value in php array code example
Example 1: array_search in php
<?php
$array = array(0 => 'blue', 1 => 'red', 2 => 'green', 3 => 'red');
$key = array_search('green', $array);
$key = array_search('red', $array);
?>
Example 2: array search by key in php
$arr = array(
"one" => 1,
"two" => 2,
"three" => 3,
"seventeen" => 17
);
function find($mot){
global $arr;
$ok=false;
foreach ($arr as $k => $v)
{
if($k==$mot){
return $v; $ok=true;
}
}
if(ok==false){ return -1; }
}
echo find("two");