English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Basic PHP Tutorial

Advanced PHP Tutorial

PHP & MySQL

PHP Reference Manual

Usage and Example of PHP array_udiff_assoc() Function

PHP Array Function Manual

The array_udiff_assoc() function is used to compare the key names and key values of two (or more) arrays and return the difference set.

Syntax

array_udiff_assoc ( $array1, $array2 , $array3 ..., $data_compare_func] );

Definition and Usage

This comparison is done through the callback function provided by the user. You must return an integer less than, equal to, or greater than zero when you think the first parameter is less than, equal to, or greater than the second parameter.

It calculates the difference of the array using additional index checks, compares the data through the callback function, and returns an array that includes1That includes all values in the array, which do not appear in any other parameter.

Parameter

NumberParameters and Description
1

array1(Required)

It specifies an array.

2

array2(Required)

It specifies the array to be compared with the first array.

3

array3(Optional)

It specifies the array to be compared with the first array.

4

data_compare_func*(Required)

Name of the user-defined function.

Return Value

array_udiff_assoc() returns an array that includes all values in the array1 The values are in the first array but not in any other parameter array. Note that unlike array_diff() and array_udiff(), the key names are also used for comparison. The comparison of array data is done using the callback function provided by the user. In this respect, it is the opposite of array_diff_assoc(), which uses an internal function for comparison.

Online Example

array_udiff_assoc() uses a custom callback function to compare the difference between two arrays

<?php
   function call_back_function($v1,2) {
      if ($v1 === $v2) {
         return 0;
      }
      return 1;
   }
   $input = array("a"=>"orange","b"=>"orange","c"=>"mango");
   $input1 = array("a"=>"orange","b"=>"mango","c"=>"orange");
   
   print_r(array_udiff_assoc($input,$input1,"call_back_function");
?>
Test and see‹/›

Output Result:

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

   PHP Array Function Manual