Using PHP's array_uintersect() to Compute Array Intersection with a Callback Function

This article explains the PHP function array_uintersect(), which returns the intersection of arrays using a user‑defined callback for value comparison, describes its parameters and return value, and provides a complete example with code and expected output.

Laravel Tech Community
Laravel Tech Community
Laravel Tech Community
Using PHP's array_uintersect() to Compute Array Intersection with a Callback Function

The array_uintersect() function returns an array containing all values that exist in the first array and also appear in all other arrays passed as arguments, where the comparison of values is performed by a user‑provided callback function. The callback must return an integer less than, equal to, or greater than zero when the first argument is considered less than, equal to, or greater than the second argument.

Parameters

array1 : The first array.

array2 : The second array (additional arrays can be supplied).

data_compare_func : A callable that compares two values and returns an integer (<0, 0, >0) indicating their ordering.

Return value

An array containing all values from array1 that are also present in every other array argument, according to the comparison performed by the callback.

Example

<?php
$array1 = array("a" => "green", "b" => "brown", "c" => "blue", "d" => "red");
$array2 = array("a" => "GREEN", "b" => "brown", "c" => "yellow", "d" => "red");
print_r(array_uintersect($array1, $array2, "strcasecmp"));
?>

Output

Array
(
    [a] => green
    [b] => brown
    [0] => red
)
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

BackendPHPArraycallbackfunctionIntersection
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

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.