Easy Tutorial
❮ Filter Validate Ip Php Isset Function ❯

PHP array_uintersect() Function

Complete PHP Array Reference Manual

Example

Compare the values of two arrays (using a user-defined function to compare values) and return the intersection:

<?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_uintersect($a1, $a2, "myfunction");
print_r($result);
?>

Definition and Usage

The array_uintersect() function is used to compare the values of two (or more) arrays and return the intersection.

Note: This function uses a user-defined function to compare values!

This function compares the values of two (or more) arrays and returns an array that contains all the values from the first array (array1) that are present in all other arrays (array2, array3, etc.).


Syntax

Parameter Description
array1 Required. The first array to compare with other arrays.
array2 Required. An array to compare against the first array.
array3,... Optional. Additional arrays to compare against the first array.
myfunction Required. A string that defines a callable comparison function. The comparison function must return an integer less than, equal to, or greater than zero if the first argument is considered to be respectively less than, equal to, or greater than the second.

Technical Details

Return Value: Returns an array containing all the values in array1 that are present in all of the other arrays.
PHP Version: 5+
--- ---

More Examples

Example 1

Use the built-in function strcasecmp to perform a case-insensitive comparison and return the intersection:

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

print_r(array_uintersect($array1, $array2, "strcasecmp"));
?>

❮ Filter Validate Ip Php Isset Function ❯