Array Functions

PHP array_intersect_uassoc() Function

Topic: PHP Array ReferencePrev|Next

Description

The array_intersect_uassoc() function compares the values of two or more arrays and returns the matches with additional key check using a user-defined key comparison function.

The following table summarizes the technical details of this function.

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

Syntax

The basic syntax of the array_intersect_uassoc() function is given with:

array_intersect_uassoc(array1, array2, ..., key_compare_function);

The following example shows the array_intersect_uassoc() function in action.

<?php
// Define key comparison function
function compare($a, $b){
    // Converting key to lowercase
    $a = strtolower($a);
    $b = strtolower($b);

    if($a == $b){
        return 0;
    }
    return ($a < $b) ? -1 : 1;
}

// Sample arrays
$array1 = array("a"=>"apple", "b"=>"ball", "c"=>"cat", "dog");
$array2 = array("A"=>"APPLE", "B"=>"ball", "c"=>"camel");

// Computing the intersection
$result = array_intersect_uassoc($array1, $array2, "compare");
print_r($result);
?>

Note: The key comparison function must return an integer equal to zero if both keys are equal, an integer less than zero if the first key is less than the second key, and an integer greater than zero if the first key is greater than the second key.

Alternatively, you can simply use the PHP built-in strcasecmp() function which perform case-insensitive string comparison and returns < 0 if str1 is less than str2; > 0 if str1 is greater than str2, and 0 if they are equal. Therefore, the above example can be rewritten as:

<?php
// Sample arrays
$array1 = array("a"=>"apple", "b"=>"ball", "c"=>"cat", "dog");
$array2 = array("A"=>"APPLE", "B"=>"ball", "c"=>"camel");

// Computing the intersection
$result = array_intersect_uassoc($array1, $array2, "strcasecmp");
print_r($result);
?>

Parameters

The array_intersect_uassoc() function accepts the following parameters.

Parameter Description
array1 Required. Specifies the array to compare from.
array2 Required. Specifies an array to compare against.
... Optional. Specifies more arrays to compare against.
key_compare_function Required. Specifies a callback function to use for key comparison.

More Examples

Here're some more examples showing how array_intersect_uassoc() function actually works:

The following example demonstrates how to compare the keys and values of three arrays and get the intersection or matches using a user-supplied key comparison function.

<?php
// Sample arrays
$array1 = array("a"=>"apple", "b"=>"ball", "c"=>"cat", "dog");
$array2 = array("A"=>"ant", "B"=>"ball", "C"=>"camel");
$array3 = array("a"=>"airplane", "b"=>"ball");

// Computing the intersection
$result = array_intersect_uassoc($array1, $array2, $array3, "strcasecmp");
print_r($result);
?>
Bootstrap UI Design Templates Property Marvels - A Leading Real Estate Portal for Premium Properties