array key search in php code example
Example 1: php key in array exists
<?php
$search_array = array('first' => null, 'second' => 4);
// returns false
isset($search_array['first']);
// returns true
array_key_exists('first', $search_array);
?>
Example 2: array search by key in php
$arr = array(
"one" => 1,
"two" => 2,
"three" => 3,
"seventeen" => 17
);
function find($mot){
global $arr; // this is global variable
$ok=false;
foreach ($arr as $k => $v)
{
if($k==$mot){
return $v; $ok=true; // or return true;
}
}
if(ok==false){ return -1; } // or return false;
}
//call function
echo find("two");