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

Tutorial básico PHP

Tutorial avançado PHP

PHP & MySQL

Manual de referência PHP

Uso e exemplo da função PHP array_udiff_assoc()

PHP Array Function Manual

The array_udiff_assoc() function is used to compare the keys and 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 if 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 containing1of all the values in the array, which do not appear in any other parameter.

Parameter

NumberParameters and descriptions
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 the values in the array1 The values that 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 regard, it is exactly the opposite of array_diff_assoc(), which uses an internal function for comparison.

Online example

Using custom callback function to compare the difference set of two arrays with array_udiff_assoc

<?php
   function call_back_function($v1$v2) {
      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