check if a value exists in array

daniel__ picture daniel__ · Nov 21, 2011 · Viewed 8.5k times · Source

I am trying this code to check if a value exists in an array.

$arr = array ('2' => '0', '3' => '0.58');

$num=3;
if (array_key_exists($num, $arr)) {
    echo (array_key_exists($num, $arr)); //show the index, in this case 1
}

What i want is show the correspondent value, in other words, 0.58

How can i do that ?

Answer

Alex picture Alex · Nov 21, 2011

What you need is this:

$arr = array ('2' => '0', '3' => '0.58');

$num=3;
if (array_key_exists($num, $arr)) {
    echo $arr[$num];
}