note 113353 added to function.array-intersect-ukey
| From: | gk at anuary dot com | Date: | Mon, 30 Sep 2013 16:52:12 +0000 |
| Subject: | note 113353 added to function.array-intersect-ukey | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-196429@lists.php.net to get a copy of this message | ||
"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) > 0 ? 0 : -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($args, 0, 2, [$arr_unmatched]);
return array_merge($arr_matched, call_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) ""
}*/
?>
----
Server IP: 69.147.83.199
Probable Submitter: 91.85.156.201
----
Manual Page -- http://php.net/manual/en/function.array-intersect-ukey.php
Edit -- https://master.php.net/note/edit/113353
Del: integrated -- https://master.php.net/note/delete/113353/integrated
Del: useless -- https://master.php.net/note/delete/113353/useless
Del: bad code -- https://master.php.net/note/delete/113353/bad+code
Del: spam -- https://master.php.net/note/delete/113353/spam
Del: non-english -- https://master.php.net/note/delete/113353/non-english
Del: in docs -- https://master.php.net/note/delete/113353/in+docs
Del: other reasons-- https://master.php.net/note/delete/113353
Reject -- https://master.php.net/note/reject/113353
Search -- https://master.php.net/manage/user-notes.php