PHP array_uintersect_assoc: Compute Array Intersection with Index Check Using a Callback

The article explains PHP's array_uintersect_assoc function, which returns the intersection of two arrays while preserving keys and using a user‑provided callback to compare values, and includes a detailed parameter description, return value, and a complete runnable example.

Laravel Tech Community
Laravel Tech Community
Laravel Tech Community
PHP array_uintersect_assoc: Compute Array Intersection with Index Check Using a Callback

Function signature

array array_uintersect_assoc (array $array1, array $array2, callable $data_compare_func)

Description

This function computes the intersection of $array1 and $array2 while preserving the keys of the first array. Comparison of values is performed by a user‑supplied callback function $data_compare_func, which 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.

data_compare_func : A callable that receives two values and returns an integer (<0, 0, >0) indicating the comparison result.

Return value

Returns an array containing all values from $array1 that are also present in $array2 (according to the comparison callback), preserving the original keys.

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_assoc($array1, $array2, "strcasecmp"));
?>

Output

Array
(
    [a] => green
)
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.

callbackarray intersectionarray_uintersect_assoc
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.