188 of 410 menu

The array_uintersect Function

The array_uintersect function computes the intersection of arrays using a callback function for value comparison. It returns an array containing all the values from the first array that are present in all the other arrays. Value comparison is done through a user-defined callback function.

Syntax

array_uintersect(array $array1, array $array2, ..., callable $value_compare_func): array;

Example

Find the intersection of two arrays by comparing their values using a callback function:

<?php $array1 = [1, 2, 3, 4, 5]; $array2 = [2, 4, 6, 8, 10]; $res = array_uintersect($array1, $array2, function($a, $b) { if ($a === $b) { return 0; } return ($a > $b) ? 1 : -1; }); print_r($res); ?>

Code execution result:

[2, 4]

Example

Comparing string arrays using a custom function:

<?php $array1 = ['a', 'b', 'c', 'd', 'e']; $array2 = ['b', 'd', 'f', 'h', 'j']; $res = array_uintersect($array1, $array2, function($a, $b) { return strcmp($a, $b); }); print_r($res); ?>

Code execution result:

['b', 'd']

See Also

  • the array_intersect function,
    which computes the intersection of arrays
  • the array_intersect_assoc function,
    which computes the intersection of arrays with additional index check
  • the array_uintersect_assoc function,
    which computes the intersection of arrays with additional index check using a callback function
byenru