Example:
$animal = ("bird" => "crow", "cat" => "lion", "snake" => "cobra");
I want to get all values whose keys are
function_name_get_value(["monkey","goat","bird"]);
I tried using array_key_exists()
but the problem is I cannot pass the $animal
variable since it is specified what arguments are to be passed on function function_name_get_value
It's simple you just need to pass 2 arguments one is name of key and second is array.
function getValue($arr, $key)
{
return isset($arr[$key]) && is_array($arr) ? $arr[$key] : false;
}
if you want get more keys just use loop.
function getValue($arr, $keyArr)
{
if(!is_array($arr) || !is_array($keyArr)) return false;
$found = array();
foreach($keyArray as $key)
if(array_key_exists($key, $arr)) $found[$key] = $arr[$key];
return $found;
}
array_key_exists needs two arguments one is keyname and second is array. I don't see any other option to find a key in array. You must pass 2 parametrs at least