The Wayback Machine - https://web.archive.org/web/20231211180659/https://www.geeksforgeeks.org/php-array_udiff_assoc-function/
Open In App
Related Articles

PHP array_udiff_assoc() Function

Improve Article
Improve
Save Article
Save
Like Article
Like

The array_udiff_assoc() is an inbuilt function in PHP and used for distinguishing between two or more arrays. The function computes the difference arrays from two or more arrays by use of user-defined function with additional key and returns differences. It returns all the entries that are present in the first array and not present in any other array. It is different from array_diff_assoc() function as it allows a user-defined function to decide the criteria.

Syntax:

array_udiff_assoc($array1, $array2, $array3……….array nth, arr_udiffassocFun)

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

  1. array1 : It is the initial array and compares to another array. It is Mandatory.
  2. array2 : The array Compared with the first array key. It is Mandatory .
  3. array3 : The second array Compared with the first array key. It is Optional.
  4. arr_udiffassocFun: It’s 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 which contains elements of first array which are not present in all others arrays. If there are all array element present then array return NULL.
Note: This built-in function array_udiff_assoc()) to compare the keys of an array and user-defined function to compare the values. The function checks only one dimension of a n-dimensional array.

Example 1 :

Input :
 $arr1 = array(
    "a" => "Geeks",
    "b" => "for",
    "d" => "geeks"
);
$arr2 = array(
    "a" => "Geeks",
    "y" => "is",
    "d" => "geeks"
);
Output:
Array
(
    [b] => for
)
Explanation: arr1 contains only one 
values(for) which is not present in 
arr2.

Example 2 :

Input:
$arr1 = array(
    "a" => "C",
    "b" => "C++",
    "d" => "Java",
    "x" => "XML",
    "y" => "C#"
);
$arr2 = array(
    "a" => "C",
    "b" => "C++",
    "d" => "PHP",
    "x" => "Advanced PHP",
    "n" => "XML"    
);
Output:
Array
(
    [d] => Java
    [x] => XML
    [y] => C#
)

Explanation: arr1  return three values
(article) which are not present in arr2.
But XML is present both arr1 and arr2 
but both have different key.

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

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




<?php
// PHP code for array_udiff_assoc function.
  
// This function is used to decide which elements
// to pick array_udiff_assoc
function arr_udiffassocFun($x, $y)
{       
    return ($x === $y)? 0 : 1;
}
  
// array list for comparison.
$arr1 = array(
    "a" => "Raj",
    "b" => "Ram",
    "d" => "Denish",
    "r" => "David"
);
$arr2 = array(
    "a" => "Raj",
    "y" => "Ram",
    "d" => "Denish",
    "x" => "Ritche"
);
  
// Driver code
$result = array_udiff_assoc($arr1,
             $arr2, "arr_udiffassocFun");
print_r($result);
?>


Output:

Array
(
    [b] => Ram
    [r] => David
)

Note: [b] => Ram is printed because key is different. In array_diff_assoc() and array_udiff_assoc(), keys and values both are compared. array_diff() compares only values.

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




<?php
// PHP code for array_udiff_assoc function
// This function is used to decide which elements
// to pick array_udiff_assoc
function arr_udiffassocFun($x, $y)
{       
    return ($x === $y)? 0 : 1;
}
  
// array list for comparison.
$arr1 = array(
    "a" => "Larry",
    "b" => "Page",
    "d" => "Denish",
    "r" => "Ritche"
);
$arr2 = array(
    "a" => "Larry",
    "y" => "Page",
    "d" => "Denish",
    "r" => "Ritche"
);
$arr3 = array(
    "a" => "larry",
    "y" => "Bill Gate",
    "d" => "Denish",
    "r" => "Willion"
);
$arr4 = array(
    "a" => "Raj",
    "y" => "Bill Gate",
    "d" => "Denish",
    "r" => "Woks"
);
$result = array_udiff_assoc($arr1,
   $arr2, $arr3, $arr4, "arr_udiffassocFun");
// print result.
print_r($result);
?>


Output:

Array
(
    [b] => Page
)

Related Article:

Practice Similar Questions On:PHP
References: http://php.net/manual/en/function.array-udiff-assoc.php


Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 20 Jun, 2023
Like Article
Save Article
Previous
Next
Similar Reads
Complete Tutorials