Frank Lucas Frank Lucas - 17 days ago 6
PHP Question

PHP loop over array keys and return value

I am trying to loop over the keys of an array and return the value when that key matches with a variable. But what I am doing just doesn't seem to make much sense.

public function check($variable)
{
$result = 0;

$amount = array(
3 => 10,
4 => 20,
5 => 50
);

foreach ($amount as $a) {
if ($a == $variable) {
$result = $a[$amount];
}
}

return $result;
}


At this point I am not even sure anymore if what I am doing is right :p

Anyone who could help me out?

Many thanks in advance!

Answer
public function check($variable)
{
    $result = 0;

    $amount = array(
        3 => 10,
        4 => 20,
        5 => 50
    );

    if (isset($amount[$variable])) {
        $result = $amount[$variable];
    }

    return $result;
}
Comments