Search code examples
phparraysarray-key-exists

check if a value exists in array


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 ?


Solution

  • What you need is this:

    $arr = array ('2' => '0', '3' => '0.58');
    
    $num=3;
    if (array_key_exists($num, $arr)) {
        echo $arr[$num];
    }