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

PHP Basic Tutorial

PHP Advanced Tutorial

PHP & MySQL

PHP Reference Manual

PHP array_udiff() Function Usage and Example

PHP Array Function Manual

array_udiff — Calculate the difference set of the array using a callback function to compare data

Syntax

array_udiff($array1, $array2 [, $array3 ..., $data_compare_func]);

Definition and Usage

It uses a callback function for data comparison to calculate the difference of the array and returns an array, including array1all values that do not exist in all other parameters. Unlike array_diff(), the former uses built-in functions for data comparison.

Parameter

Serial NumberParameters and Description
1Parameter

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

It returns an array, returning array1 does not appear in other parameters.

Online Example

Using a custom function to compare two arrays

<?php
function myfunction($a,$b)
{
    if ($a === $b)
    {
        return 0;
    }
    return ($a > $b)?1:-1;
}
$a1= array("a" => "red","b" => "green","c" => "blue");
$a2= array("a" => "blue","b" => "black","e" => "blue");
$result = array_udiff($a1$a2,"myfunction");
print_r($result);
?>
Test and See‹/›

Output Result:

Array
(
    [a] => red
    [b] => green
)

   PHP Array Function Manual