Monday, November 18, 2024
Google search engine
HomeLanguagesPHP array_udiff() Function

PHP array_udiff() Function

The array_udiff() is an inbuilt function in PHP and used for distinguishing between two or more array. The function compares the different values of two or more arrays by the use of user-defined function data comparison and returns the differences. Actually, the function returns all the entries that are present in the first array which is not present in others array. If there are all values of all array are the same then, return NULL array. Its unlike comparison of arr_diff()

Syntax:

array_udiff($array1, $array2, $array3……….array nth, arr_udiffFunction)

Parameters Used: This array_udiff() function parameters are described below:

  1. array1 :
    It is the initial array and compares to another array…
    It’s Mandatory.
  2. array2 :
    The array Compared with the first array key.
    It’s Mandatory.
  3. array3… :
    The second array Compared with the first array key.
    Its Optional.
  4. arr_udiffFunction: Its Required user-defined function and A string that define user-defined callback function and it return an integer <, =, or > than 0 if the first argument is <, =, or > than the second argument.

Return Value:
Returns an array types values which contain the first array which is present in all others array. If there are all of the same then array returns NULL array.

Note: This built-in function (array_udiff()) to compare the keys of an array and user-defined function to compare the values.

Example 1 :


Input :
    $arr1=array("a"=>"Geeksforneveropen", "b"=>"IDE", "d"=>"environment", "v"=>"Code Run");
    $arr2=array("a"=>"Geeksforneveropen", "b"=>"Editor", "f"=>"Write", "h"=>"Code", );
    
Output:

Array
(
    [b] => IDE
    [d] => environment
    [v] => Code Run
)

Explanation: arr1 contains only three values(IDE, environment, 
             Code Run)  which is not present in arr2.

Example 2 :

Input:
$arr1 = array("a"=>"Geeks", "x"=>"article", 
                               "n"=>"Geeks");
$arr2 = array("x"=>"Geeks", "y"=>"for", "z"=>"Geeks", 
               "a"=>"IDE", "a"=>"Editor", );
Output:

Array
(
    [x] => article
)

Explanation: arr1 contains only one value (article) which is not present in arr2.

Let’s take a simple example to understand array_udiff() Function.

Program 1: Take two array (array1 and array2) and using user-defined key comparison function (arr_udiffFunction).




<?php
//PHP code for array_udiff function
function arr_udiffFunction($a, $b)
{
    if ($a === $b) {
        return 0;
    }
    return ($a > $b) ? 1 : -1;
}
//array list for comparison.
$arr1 = array(
    "a" => "C",
    "b" => "C++",
    "d" => "Java",
    "r" => "XML"
);
$arr2 = array(
    "a" => "C",
    "y" => "C++",
    "d" => "C#",
    "x" => "PHP"
);
  
$result = array_udiff($arr1, $arr2, "arr_udiffFunction");
//print result.
print_r($result);
?>


Output:

Array
(
    [d] => Java
    [r] => XML
)

Program: 2 Take four array (array1, array2, array3 and array4 ) and using user-defined key comparison function array_udiff().




<?php
//PHP code for array_udiff function
function arr_udiffFunction($a, $b)
{
    if ($a === $b) {
        return 0;
    }
    return ($a > $b) ? 1 : -1;
}
//array list for comparison.
$arr1 = array(
    "a" => "C lab",
    "b" => "C++ lab",
    "d" => "Java lab",
    "r" => "ASP lab"
);
$arr2 = array(
    "a" => "C lab",
    "y" => "C++ lab",
    "d" => "Java lab",
    "r" => "XML lab"
);
$arr3 = array(
    "a" => "C# lab",
    "n" => "Android lab",
    "d" => "Python lab",
    "r" => "XML lab"
);
$arr4 = array(
    "a" => "Objective C lab",
    "y" => "C++ lab",
    "d" => "Java lab",
    "r" => "Perl lab"
);
  
$result = array_udiff($arr1, $arr2, $arr3, "arr_udiffFunction");
//print result.
print_r($result);
?>


Output:

Array
(
    [r] => ASP lab
)

Program: 3 Take two array (array1 and array2) and using user-defined key comparison function (array_udiff(). which has match all element and values of array which is return NULL.




<?php
//PHP code for array_udiff function
function arr_udiffFunction($a, $b)
{
    if ($a === $b) {
        return 0;
    }
    return ($a > $b) ? 1 : -1;
}
//array list for comparison.
$arr1 = array(
    "a" => "C",
    "b" => "C++",
    "d" => "Java",
    "r" => "XML"
);
$arr2 = array(
    "a" => "C",
    "y" => "C++",
    "d" => "Java",
    "r" => "XML"
);
  
$result = array_udiff($arr1, $arr2, "arr_udiffFunction");
//print result.
print_r($result);
?>


Output:

Array
(
)

RELATED ARTICLES

Most Popular

Recent Comments