Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6682359
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:41:48+00:00 2026-05-26T04:41:48+00:00

I need to compare database values with post values. If post values (decimal price)

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T04:41:48+00:00Added an answer on May 26, 2026 at 4:41 am

    http://docs.php.net/array_udiff:

    The user supplied callback function is used for comparison. It must return an integer less than, equal to, or greater than zero if the first argument is considered to be respectively less than, equal to, or greater than the second.

    Your function always returns 1 if the elements are unequal which results in diffrent (wrong) comparisons decisions.

    <?php
    function epsilon_compare ($v1,$v2)
    {
        $epsilon = 0.02;
        $diff = $v1 - $v2;
        return abs($diff)<=$epsilon ? 0 : $diff;
    }
    
    $db_values =   array( "21" => 10.00, "22" => 20.00, "25" => 3.55);
    $post_values = array( "21" => 8.00,  "22" => 20.01, "25" => 2.55 );
    print_r(array_udiff($post_values, $db_values, "epsilon_compare"));
    

    prints

    Array
    (
        [21] => 8
        [25] => 2.55
    )
    

    edit: using array_udiff_assoc

    $db_values = array ( "1" => 7.55, "2" => 5.45, "3" => 5.45, "4" => 64.45, "5" => 54.75, "6" => 30.40, "7" => 56.99, "8" => 10.90, "9" => 60.85, "11" => 3.25, "12" => 13.05, "13" => 5.45, "14" => 8.00, "15" => 5.45, "16" => 13.05, "17" => 4.35 );
    $post_values = array ( "1" => 7.55, "2" => 5.45, "3" => 5.45, "4" => 64.45, "5" => 54.75, "6" => 30.40, "7" => 56.99, "8" => 10.90, "9" => 60.85, "11" => 3.25, "12" => 13.05, "13" => 2.45, "14" => 8.00, "15" => 5.45, "16" => 12.05, "17" => 2.34 );
    print_r(array_udiff_assoc($post_values, $db_values, "epsilon_compare"));
    

    prints

    Array
    (
        [13] => 2.45
        [16] => 12.05
        [17] => 2.34
    )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to compare the data coming from a database field with values from
I need to compare 2 strings as equal such as these: Lubeck == Lübeck
I need to compare some Rails (2.3.11) model attribute values before and after a
I need to compare strings in shell: var1=mtu eth0 if [ $var1 == mtu
I need to compare the integer part of two doubles for inequality and I'm
I need to compare the answer in with the aspnet_membership tables PasswordAnswer value. The
I need to compare build outputs of VS2005 in order to be sure I
I need to compare 2 strings in C# and treat accented letters the same
I need to compare dozens of fields in two objects (instances of the same
I need to compare two Date s (e.g. date1 and date2 ) and come

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.