Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Compute difference of arrays using a user defined function

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 364
    Comment on it

    My previous article How to find the difference of arrays in PHP demonstrate about some predefined PHP functions such as array_diff(), array_diff_key() and array_diff_assoc() which are used to compute the difference of two or more arrays. These functions use internal function for data comparison. This article demonstrates about a new version of these functions which requires a user-defined function for data comparison. The list and details of functions are given below :

     

    1. array_udiff()

    The array_udiff() function computes the difference between two or more arrays by comparing their values using a user-defined function. Just like array_diff() this function also returns an array of unmatched elements of the first array which are not present in other arrays. The only difference is this function required a callback function to compare values of arrays.

     

    Syntax : array_udiff($arr1,$arr2,$arr3.....,"comparison_function")

    $arr1 : Specify an array which will be compared with other arrays.(Required)

    $arr2 : An array compared with $array1.(Required)

    $arr3 : An array compared with $array1.(Optional)

    comparison_function : Specifies a user defined function for comparing arrays elements.(Required)

     

    Result : array_udiff() function return an array of elements from $arr1 which are not present in other arrays after comparing array values using a user-defined callback function.

     

    Example :

    <?php
    $arr1 = array("a" => "orange", "1" => "apple", "c" => "mango", "2" => "banana");
    $arr2 = array("a" => "peach", "b" => "orange", "c" => "mango", "d" => "banana");
    print_r(array_udiff($arr1,$arr2,"comp_data"));
    
    function comp_data($a,$b)
    {
     if ($a===$b){
      return 0;
     } else {
      return ($a>$b)?1:-1;
     }
    }
    ?>

     

    Output :

    Array ( [1] => apple [2] => pear )

     

     

    2. array_diff_ukey() :

    array_diff_ukey() function also compute the difference of two or more arrays by using a user defined function for comparing keys of arrays. This function also computes difference same like array_diff_key(), the only difference is this function uses a user defined callback function for comparison of key.

     

    Syntax : array_diff_ukey($arr1,$arr2,$arr3...,"comparison_function")

    $arr1 : Specify an array which will be compared with other arrays.(Required)

    $arr2 : An array compared with $array1.(Required)

    $arr3 : An array compared with $array1.(Optional)

    comparison_function : Specifies a user defined function for comparing array elements.(Required)

     

    Result : This function returns an array of elements from $arr1 which are not present in other arrays after comparing keys by user-defined callback function.

     

    Example :

    <?php
    $arr1 = array("a" => "orange", "1" => "apple", "c" => "mango", "2" => "pear");
    $arr2 = array("2" => "peach", "c" => "orange", "3" => "mango", "d" => "banana");
    $arr3 = array("3" => "peach", "b" => "orange", "1" => "mango", "d" => "banana");
    $result = array_diff_ukey($arr1, $arr2, $arr3, "compare");
    print_r($result);
    
    function compare($a,$b)
    {
     if ($a===$b){
      return 0;
     } else {
      return ($a>$b)?1:-1;
     }
    }
    
    ?>

    Output :

    Array ( [a] => orange [1] => apple )

     

     

    3. array_diff_uassoc()

    array_diff_uassoc() function computes the difference of two or more arrays by using a user defined callback function for comparing both keys and values. This function uses a user-defined callback function for comparing keys and values of the first array with other arrays and return an array of elements from the first array which are not present in other arrays.

     

    Syntax : array_diff_uassoc($arr1,$arr2,$arr3...,"compare_function")

    $arr1 : Specify an array which will be compared with other arrays.(Required)

    $arr2 : An array compared with $array1.(Required)

    $arr3 : An array compared with $array1.(Optional)

    comparison_function : Specifies a user defined function for comparing array elements.(Required)

     

    Result : This function returns an array containing elements from $arr1 after comparing keys and values of $arr1 with other arrays which are not found in other arrays.

     

    Example :

    <?php
    $arr1=array("a"=>"apple","b"=>"orange","c"=>"pear","d"=>"mango");
    $arr2=array("a"=>"apple","c"=>"orange","g"=>"pear");
    $arr3=array("g"=>"pear","a"=>"apple","c"=>"pear");
    $result=array_diff_uassoc($arr1,$arr2,$arr3,"compare");
    print_r($result);
    
    function compare($a,$b)
    {
     if ($a===$b){
      return 0;
     } else {
      return ($a>$b)?1:-1;
     }
    }
    ?>

    Output :

    Array ( [b] => orange [d] => mango )

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: