array_intersect_ukey

(PHP 5 >= 5.1.0)

array_intersect_ukeyComputes the intersection of arrays using a callback function on the keys for comparison

Description

array array_intersect_ukey ( array $array1 , array $array2 [, array $... ], callable $key_compare_func )

array_intersect_ukey() returns an array containing all the values of array1 which have matching keys that are present in all the arguments.

Parameters

array1

Initial array for comparison of the arrays.

array2

First array to compare keys against.

...

Variable list of array arguments to compare keys against.

key_compare_func

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.

int callback ( mixed $a, mixed $b )

Return Values

Returns the values of array1 whose keys exist in all the arguments.

Examples

Example #1 array_intersect_ukey() example

<?php
function key_compare_func($key1$key2)
{
    if (
$key1 == $key2)
        return 
0;
    else if (
$key1 $key2)
        return 
1;
    else
        return -
1;
}

$array1 = array('blue'  => 1'red'  => 2'green'  => 3'purple' => 4);
$array2 = array('green' => 5'blue' => 6'yellow' => 7'cyan'   => 8);

var_dump(array_intersect_ukey($array1$array2'key_compare_func'));
?>

The above example will output:

array(2) {
  ["blue"]=>
  int(1)
  ["green"]=>
  int(3)
}

In our example you see that only the keys 'blue' and 'green' are present in both arrays and thus returned. Also notice that the values for the keys 'blue' and 'green' differ between the two arrays. A match still occurs because only the keys are checked. The values returned are those of array1.

See Also

  • array_diff() - Computes the difference of arrays
  • array_udiff() - Computes the difference of arrays by using a callback function for data comparison
  • array_diff_assoc() - Computes the difference of arrays with additional index check
  • array_diff_uassoc() - Computes the difference of arrays with additional index check which is performed by a user supplied callback function
  • array_udiff_assoc() - Computes the difference of arrays with additional index check, compares data by a callback function
  • array_udiff_uassoc() - Computes the difference of arrays with additional index check, compares data and indexes by a callback function
  • array_diff_key() - Computes the difference of arrays using keys for comparison
  • array_diff_ukey() - Computes the difference of arrays using a callback function on the keys for comparison
  • array_intersect() - Computes the intersection of arrays
  • array_intersect_assoc() - Computes the intersection of arrays with additional index check
  • array_intersect_uassoc() - Computes the intersection of arrays with additional index check, compares indexes by a callback function
  • array_intersect_key() - Computes the intersection of arrays using keys for comparison

Коментарии

"array_intersect_ukey" will not work if $key_compare_func is using regular expression to perform comparison. "_array_intersect_ukey" fully implements the "array_intersect_ukey" interface and handles properly boolean comparison. However, the native implementation should be preferred for efficiency reasons.

<?php
$key_compare_func 
= function ($a$b) {
   
var_dump('a=' $a'; b=' $b);
   
    if (
strpos($b'/') === 0) {
        return 
preg_match($b$a) > : -1;
    }
   
    if (
$a == $b) {
       
$weight 0;
    } else if (
$a $b) {
       
$weight 1;
    } else {
       
$weight = -1;
    }
   
   
var_dump('literal comparison: ' $weight);
   
    return 
$weight;
};

$foo = ['aab' => '''bbb' => '''ccb' => '''abb' => ''];
$bar = ['/[a-z]b[a-z]/' => ''];
$buz = ['/c[a-z][a-z]/' => ''];

echo 
PHP_EOL 'array_intersect_ukey' PHP_EOL PHP_EOL;

$subject array_intersect_ukey ($foo$bar$buz$key_compare_func);

echo 
PHP_EOL;

var_dump($subject);

echo 
PHP_EOL '_array_intersect_ukey' PHP_EOL PHP_EOL;

$subject _array_intersect_ukey($foo$bar$buz$key_compare_func);

echo 
PHP_EOL;

var_dump($subject);

/**
 * @author Gajus Kuizinas <gk@anuary.com>
 * @version 1.0.0 (2013 09 30)
 * @url https://gist.github.com/gajus/271ad5f36337a32a184c
 */
function _array_intersect_ukey (array $arr1, array $arr2$key_compare_func) {
   
$arr_matched = [];
   
$arr_unmatched = [];
   
   
$args func_get_args();
   
   
$key_compare_func end($args);
   
    foreach (
$arr1 as $k1 => $v1) {
        foreach (
$arr2 as $k2 => $v2) {
           
$diff $key_compare_func($k1$k2);
       
           
//var_dump('k1=' . $k1 . ', k2=' . $k2 . ', diff=' . $diff);
           
           
if ($diff === 0) {
               
$arr_matched[$k1] = $v1;
            } else {
               
$arr_unmatched[$k1] = $v1;
            }
        }
    }
   
    if (
count($args) <= 3) {
        return 
$arr_matched;
    }
   
   
array_splice($args02, [$arr_unmatched]);
   
    return 
array_merge($arr_matchedcall_user_func_array('_array_intersect_ukey'$args));
}

/*
array_intersect_ukey

string(12) "a=bbb; b=aab"
string(21) "literal comparison: 1"
string(12) "a=bbb; b=ccb"
string(22) "literal comparison: -1"
string(12) "a=abb; b=bbb"
string(22) "literal comparison: -1"
string(12) "a=aab; b=abb"
string(22) "literal comparison: -1"
string(22) "a=aab; b=/[a-z]b[a-z]/"
string(22) "a=abb; b=/[a-z]b[a-z]/"
string(22) "a=abb; b=/c[a-z][a-z]/"

array(0) {
}

_array_intersect_ukey

string(22) "a=aab; b=/[a-z]b[a-z]/"
string(22) "a=bbb; b=/[a-z]b[a-z]/"
string(22) "a=ccb; b=/[a-z]b[a-z]/"
string(22) "a=abb; b=/[a-z]b[a-z]/"
string(22) "a=aab; b=/c[a-z][a-z]/"
string(22) "a=ccb; b=/c[a-z][a-z]/"

array(3) {
  ["bbb"]=>
  string(0) ""
  ["abb"]=>
  string(0) ""
  ["ccb"]=>
  string(0) ""
}*/
?>
2013-09-30 19:52:10
http://php5.kiev.ua/manual/ru/function.array-intersect-ukey.html

    Поддержать сайт на родительском проекте КГБ