Backend Development 4 min read

array_udiff_uassoc() – Compute Array Difference with Index Check Using Callback Functions

array_udiff_uassoc() returns an array containing values from the first array that are not present in any of the other arrays, comparing both values and keys using user‑defined callback functions; the article explains its parameters, behavior, and provides a complete PHP example demonstrating usage.

Laravel Tech Community
Laravel Tech Community
Laravel Tech Community
array_udiff_uassoc() – Compute Array Difference with Index Check Using Callback Functions

Function signature

array_udiff_uassoc (array $array1, array $array2, callable $data_compare_func, callable $key_compare_func)

Description

The array_udiff_uassoc() function returns an array consisting of all values from $array1 that are not present in any of the other arrays, while also comparing the array keys (indexes) using a user‑defined callback. This allows custom comparison logic for both data values and their associated keys.

Parameters

$array1 : The first (base) array.

$array2 : The second array to compare against (additional arrays can be supplied).

$data_compare_func : A callable that receives two values and must return an integer less than, equal to, or greater than zero when the first value is considered respectively less than, equal to, or greater than the second.

$key_compare_func : A callable that receives two keys (indexes) and returns an integer in the same manner as $data_compare_func . This key comparison differentiates array_udiff_uassoc() from array_udiff_assoc() , which uses internal key comparison.

Return value

An array containing the values from $array1 that are not present in any of the other supplied arrays after applying both the data and key comparison callbacks.

Example

<?php
class cr {
    private $priv_member;
    function cr($val) {
        $this->priv_member = $val;
    }
    static function comp_func_cr($a, $b) {
        if ($a->priv_member === $b->priv_member) return 0;
        return ($a->priv_member > $b->priv_member) ? 1 : -1;
    }
    static function comp_func_key($a, $b) {
        if ($a === $b) return 0;
        return ($a > $b) ? 1 : -1;
    }
}

$a = array(
    "0.1" => new cr(9),
    "0.5" => new cr(12),
    0      => new cr(23),
    1      => new cr(4),
    2      => new cr(-15)
);

$b = array(
    "0.2" => new cr(9),
    "0.5" => new cr(22),
    0      => new cr(3),
    1      => new cr(4),
    2      => new cr(-15)
);

$result = array_udiff_uassoc(
    $a,
    $b,
    array('cr', 'comp_func_cr'),
    array('cr', 'comp_func_key')
);

print_r($result);
?>

Output

Array
(
    [0.1] => cr Object
        (
            [priv_member] => 9
        )
    [0.5] => cr Object
        (
            [priv_member] => 12
        )
    [0] => cr Object
        (
            [priv_member] => 23
        )
)
backendPHPArraycallbackfunctionphp-array-functions
Laravel Tech Community
Written by

Laravel Tech Community

Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.