I need to compare database values with post values. If post values (decimal price) are within 2 cents threshold consider values equal. Result is array with 'real' difference. Arrays are consistent: same number of values, same keys.
$db_values = array( "21" => 10.00, "22" => 20.00, "25" => 3.55);
$post_values = array( "21" => 9.98, "22" => 20.01, "25" => 2.55 );
I'm trying to compare Absolute value to my tolerance value -- epsilon (Compare decimals in PHP) and array_udiff:
function epsilon_compare ($v1,$v2)
{
$epsilon = 0.02;
$diff = abs($v1 - $v2);
if ($diff <= $epsilon)
{
return 0;
//echo "numbers are equal";
} else {
return 1;
}
}
print_r(array_udiff($post_values, $db_values, "epsilon_compare"));
gives correct result: Array ( [25] => 2.55 )
but when i use different array I get wrong result eg:
$db_values = array( "21" => 10.00, "22" => 20.00, "25" => 3.55);
$post_values = array( "21" => 8.00, "22" => 20.01, "25" => 2.55 );
In this case it gives:
Array ( [21] => 8 [22] => 20.01 [25] => 2.55 )
Key [22] => 20.01 is listed but it is within the threshold so it shouldn't be in result set. I think I don't fully understand array_udiff. thanks.