Array Functions
Advertisements

PHP array_intersect_ukey() Function

Topic: PHP Array ReferencePrev|Next

Description

The array_intersect_ukey() function compares the keys of two or more arrays and returns the matches or intersection using a user-defined key comparison function.

Also, the values for the keys are not considered in the comparison, only the keys are checked.

The following table summarizes the technical details of this function.

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

Syntax

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

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

The following example shows the array_intersect_ukey() 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");
$array2 = array("A"=>"airplane", "x"=>"xylophone", "y"=>"yacht");

// Computing the intersection
$result = array_intersect_ukey($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");
$array2 = array("A"=>"airplane", "x"=>"xylophone", "y"=>"yacht");

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

Parameters

The array_intersect_ukey() function accepts the following parameters.

Parameter Description
array1 Required. Specifies the array to compare from.
array2 Required. Specifies an array to compare keys against.
... Optional. Specifies more arrays to compare keys 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_ukey() function actually works:

The following example demonstrates how to compare the keys 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"=>"apricot", "b"=>"banana");
$array3 = array("a"=>"alligator", "b"=>"balloon");

// Computing the difference
$result = array_intersect_ukey($array1, $array2, $array3, "strcasecmp");
print_r($result);
?>
Advertisements
Bootstrap UI Design Templates