比较变量start和数组键

There is this array:

$array1 = array(51=>1.1, 45=>68, 57=>43, 62=>35, 74=>24);

And I want to verify if the value that is taken from the variable starts with any of the keys from the array. (the variable is passing correctly, I checked that)

foreach (array_keys($array1) as $key1) {
    if(preg_match("/^[$rvalue]/", $key1))
    {
        $positive1=true; 
        $fvalue1=$array1[$key1];
    }
    else{
        $positive1=false;
        }
}

The problem is that it runs all the array and always gives me the value of the last key, instead of one matching the variable. I'm new with regex, so might be that, don't know. Any help is appreciated.

Seems kind of complicated for a simple task. How about at direct comparison:

foreach ($array1 as $key1 => $value) {
    if (substr($rvalue, 0, strlen($key1)) == $key1)
    {
        $fvalue1 = $value;
        break;
    }
}

Just break from the loop when you find a match.

Get rid of the square brackets in the regexp. Also, you're doing the check backwards -- you want to put the keys into the regexp, and match that against the string:

if (preg_match("/^$key1/, $rvalue))

Square brackets in a regexp are used to match a single character that's any one of the characters in the bracket. So [51] matches 5 or 1, but it doesn't match the whole string 51.

You could also combine all the keys into a single regexp, using | in the regexp to specify alternatives:

$alternatives = implode('|', array_keys($array1));
if (preg_match("/^(?:$alternatives)/", $rvalue, $match)) {
    $positive1 = true;
    $fvalue1 = $array1[$match[0]];
} else {
    $positive1 = false;
}