From: Andrey Hristov Date: Wed, 21 Jul 2004 21:17:56 +0000 (+0000) Subject: add array_: X-Git-Tag: PRE_ZEND_VM_DISPATCH_PATCH~411 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=5e5d2640fbcaa093d33ed01b2c7215813adb0bf6;p=php add array_: intersect_key() intersect_ukey() diff_key() diff_ukey() The first two by a patch of Cristiano Duarte. The second two were implemented in almost the same way except one small difference. --- diff --git a/NEWS b/NEWS index 637cf25497..a84d6b7136 100644 --- a/NEWS +++ b/NEWS @@ -5,6 +5,11 @@ PHP NEWS - Fixed bug #29236 (memory error when wsdl-cache is enabled). (Dmitry) - Fixed bug #29109 (SoapFault exception: [WSDL] Out of memory). (Dmitry) - Fixed bug #29061 (soap extension segfaults). (Dmitry) +- Added new functions : + . array_diff_key() (Andrey) + . array_diff_ukey() (Andrey) + . array_intersect_key() (Christiano Duarte) + . array_intersect_ukey() (Christiano Duarte) - Added MDTM support to ftp_url_stat. (Sara) - Added zlib stream filter suport. (Sara) - Added bz2 stream filter support. (Sara) diff --git a/ext/standard/array.c b/ext/standard/array.c index 0b042142ab..87242d9df8 100644 --- a/ext/standard/array.c +++ b/ext/standard/array.c @@ -77,6 +77,7 @@ php_array_globals array_globals; #define DIFF_NORMAL 0 #define DIFF_ASSOC 1 +#define DIFF_KEY 2 #define DIFF_COMP_DATA_INTERNAL 0 #define DIFF_COMP_DATA_USER 1 #define DIFF_COMP_KEY_INTERNAL 0 @@ -84,6 +85,7 @@ php_array_globals array_globals; #define INTERSECT_NORMAL 0 #define INTERSECT_ASSOC 1 +#define INTERSECT_KEY 2 #define INTERSECT_COMP_DATA_INTERNAL 0 #define INTERSECT_COMP_DATA_USER 1 #define INTERSECT_COMP_KEY_INTERNAL 0 @@ -2797,12 +2799,17 @@ static void php_array_intersect(INTERNAL_FUNCTION_PARAMETERS, int behavior, int php_error_docref(NULL TSRMLS_CC, E_WARNING, "data_compare_type is %d. This should never happen. Please report as a bug", data_compare_type); return; } - } else if (behavior == INTERSECT_ASSOC) { + } else if ((behavior == INTERSECT_ASSOC) + ||(behavior == INTERSECT_KEY)) { + /* + INTERSECT_KEY is subset of INTERSECT_ASSOC. When having the former + no comparison of the data is done (part of INTERSECT_ASSOC) + */ intersect_key_compare_func = array_key_compare; if (data_compare_type == INTERSECT_COMP_DATA_INTERNAL && key_compare_type == INTERSECT_COMP_KEY_INTERNAL) { - /* array_intersect_assoc() */ + /* array_intersect_assoc() or array_intersect_key() */ if (argc < 2) { efree(args); @@ -2833,7 +2840,7 @@ static void php_array_intersect(INTERNAL_FUNCTION_PARAMETERS, int behavior, int } else if (data_compare_type == INTERSECT_COMP_DATA_INTERNAL && key_compare_type == INTERSECT_COMP_KEY_USER) { - /* array_intersect_uassoc() */ + /* array_intersect_uassoc() or array_intersect_ukey() */ if (argc < 3) { efree(args); @@ -2910,7 +2917,7 @@ static void php_array_intersect(INTERNAL_FUNCTION_PARAMETERS, int behavior, int *list = NULL; if (behavior == INTERSECT_NORMAL) { zend_qsort((void *) lists[i], hash->nNumOfElements, sizeof(Bucket *), intersect_data_compare_func TSRMLS_CC); - } else if (behavior == INTERSECT_ASSOC) { + } else if ((behavior == INTERSECT_ASSOC) || (behavior == INTERSECT_KEY)) { zend_qsort((void *) lists[i], hash->nNumOfElements, sizeof(Bucket *), intersect_key_compare_func TSRMLS_CC); } } @@ -2926,7 +2933,8 @@ static void php_array_intersect(INTERNAL_FUNCTION_PARAMETERS, int behavior, int /* go through the lists and look for common values */ while (*ptrs[0]) { - if (behavior == INTERSECT_ASSOC + if ((behavior == INTERSECT_ASSOC + || behavior == INTERSECT_KEY) && key_compare_type == INTERSECT_COMP_KEY_USER) { @@ -2938,12 +2946,18 @@ static void php_array_intersect(INTERNAL_FUNCTION_PARAMETERS, int behavior, int while (*ptrs[i] && (0 < (c = intersect_data_compare_func(ptrs[0], ptrs[i] TSRMLS_CC)))) { ptrs[i]++; } - } else if (behavior == INTERSECT_ASSOC) { + } else if (behavior == INTERSECT_ASSOC || behavior == INTERSECT_KEY) { while (*ptrs[i] && (0 < (c = intersect_key_compare_func(ptrs[0], ptrs[i] TSRMLS_CC)))) { ptrs[i]++; } - if (!c && *ptrs[i]) { /* this means that ptrs[i] is not NULL so we can compare */ - /* and "c==0" is from last operation */ + if ((!c && *ptrs[i]) && (behavior == INTERSECT_ASSOC)) { + /* + this means that ptrs[i] is not NULL so we can compare + and "c==0" is from last operation + in this branch of code we enter only when INTERSECT_ASSOC + since when we have INTERSECT_KEY compare of data is not + wanted. + */ if (data_compare_type == INTERSECT_COMP_DATA_USER) { BG(user_compare_func_name) = args[arr_argc]; } @@ -2951,7 +2965,7 @@ static void php_array_intersect(INTERNAL_FUNCTION_PARAMETERS, int behavior, int c = 1; if (key_compare_type == INTERSECT_COMP_KEY_USER) { BG(user_compare_func_name) = args[argc - 1]; - /* When KEY_USER the last parameter is always the callback */ + /* When KEY_USER, the last parameter is always the callback */ } /* we are going to the break */ } else { @@ -2996,7 +3010,7 @@ static void php_array_intersect(INTERNAL_FUNCTION_PARAMETERS, int behavior, int if (0 <= intersect_data_compare_func(ptrs[0], ptrs[i] TSRMLS_CC)) { break; } - } else if (behavior == INTERSECT_ASSOC) { + } else if (behavior == INTERSECT_ASSOC || behavior == INTERSECT_KEY) { /* no need of looping because indexes are unique */ break; } @@ -3012,7 +3026,7 @@ static void php_array_intersect(INTERNAL_FUNCTION_PARAMETERS, int behavior, int if (intersect_data_compare_func(ptrs[0]-1, ptrs[0] TSRMLS_CC)) { break; } - } else if (behavior == INTERSECT_ASSOC) { + } else if (behavior == INTERSECT_ASSOC || behavior == INTERSECT_KEY) { /* no need of looping because indexes are unique */ break; } @@ -3032,6 +3046,23 @@ out: efree(args); } +/* {{{ proto array array_intersect_key(array arr1, array arr2 [, array ...], callback data_compare_func) + Returns the entries of arr1 that have keys which are present in all the other arguments. Kind of equivalent to array_diff(array_keys($arr1), array_keys($arr2)[,array_keys(...)]). Equivalent of array_intersect_assoc() but does not do compare of the data. */ +PHP_FUNCTION(array_intersect_key) +{ + php_array_intersect(INTERNAL_FUNCTION_PARAM_PASSTHRU, INTERSECT_KEY, + INTERSECT_COMP_DATA_INTERNAL, INTERSECT_COMP_KEY_INTERNAL); +} + +/* {{{ proto array array_intersect_ukey(array arr1, array arr2 [, array ...], callback key_compare_func) + Returns the entries of arr1 that have keys which are present in all the other arguments. Kind of equivalent to array_diff(array_keys($arr1), array_keys($arr2)[,array_keys(...)]). The comparison of the keys is performed by a user supplied function. Equivalent of array_intersect_uassoc() but does not do compare of the data. */ +PHP_FUNCTION(array_intersect_ukey) +{ + php_array_intersect(INTERNAL_FUNCTION_PARAM_PASSTHRU, INTERSECT_KEY, + INTERSECT_COMP_DATA_INTERNAL, INTERSECT_COMP_KEY_USER); +} + + /* {{{ proto array array_intersect(array arr1, array arr2 [, array ...]) Returns the entries of arr1 that have values which are present in all the other arguments */ PHP_FUNCTION(array_intersect) @@ -3151,12 +3182,16 @@ static void php_array_diff(INTERNAL_FUNCTION_PARAMETERS, int behavior, int data_ php_error_docref(NULL TSRMLS_CC, E_WARNING, "data_compare_type is %d. This should never happen. Please report as a bug", data_compare_type); return; } - } else if (behavior == DIFF_ASSOC) { + } else if ((behavior == DIFF_ASSOC) || (behavior == DIFF_KEY)) { + /* + DIFF_KEY is subset of DIFF_ASSOC. When having the former + no comparison of the data is done (part of DIFF_ASSOC) + */ diff_key_compare_func = array_key_compare; if (data_compare_type == DIFF_COMP_DATA_INTERNAL && key_compare_type == DIFF_COMP_KEY_INTERNAL) { - /* array_diff_assoc() */ + /* array_diff_assoc() or array_diff_key() */ if (argc < 2) { efree(args); @@ -3187,7 +3222,7 @@ static void php_array_diff(INTERNAL_FUNCTION_PARAMETERS, int behavior, int data_ } else if (data_compare_type == DIFF_COMP_DATA_INTERNAL && key_compare_type == DIFF_COMP_KEY_USER) { - /* array_diff_uassoc() */ + /* array_diff_uassoc() or array_diff_ukey() */ if (argc < 3) { efree(args); @@ -3264,7 +3299,7 @@ static void php_array_diff(INTERNAL_FUNCTION_PARAMETERS, int behavior, int data_ *list = NULL; if (behavior == DIFF_NORMAL) { zend_qsort((void *) lists[i], hash->nNumOfElements, sizeof(Bucket *), diff_data_compare_func TSRMLS_CC); - } else if (behavior == DIFF_ASSOC) { + } else if (behavior == DIFF_ASSOC || behavior == DIFF_KEY) { zend_qsort((void *) lists[i], hash->nNumOfElements, sizeof(Bucket *), diff_key_compare_func TSRMLS_CC); } } @@ -3280,7 +3315,7 @@ static void php_array_diff(INTERNAL_FUNCTION_PARAMETERS, int behavior, int data_ /* go through the lists and look for values of ptr[0] that are not in the others */ while (*ptrs[0]) { - if (behavior == DIFF_ASSOC + if ((behavior == DIFF_ASSOC || behavior == DIFF_KEY) && key_compare_type == DIFF_COMP_KEY_USER) { @@ -3292,7 +3327,7 @@ static void php_array_diff(INTERNAL_FUNCTION_PARAMETERS, int behavior, int data_ while (*ptrs[i] && (0 < (c = diff_data_compare_func(ptrs[0], ptrs[i] TSRMLS_CC)))) { ptrs[i]++; } - } else if (behavior == DIFF_ASSOC) { + } else if (behavior == DIFF_ASSOC || behavior == DIFF_KEY) { while (*ptrs[i] && (0 < (c = diff_key_compare_func(ptrs[0], ptrs[i] TSRMLS_CC)))) { ptrs[i]++; } @@ -3304,19 +3339,36 @@ static void php_array_diff(INTERNAL_FUNCTION_PARAMETERS, int behavior, int data_ } break; } else if (behavior == DIFF_ASSOC) { + /* + In this branch is execute only when DIFF_ASSOC. If behavior == DIFF_KEY + data comparison is not needed - skipped. + */ if (*ptrs[i]) { if (data_compare_type == DIFF_COMP_DATA_USER) { BG(user_compare_func_name) = args[arr_argc]; } if (diff_data_compare_func(ptrs[0], ptrs[i] TSRMLS_CC) != 0) { + /* the data is not the same */ c = -1; if (key_compare_type == DIFF_COMP_KEY_USER) { BG(user_compare_func_name) = args[argc - 1]; } } else { break; + /* + we have found the element in other arrays thus we don't want it + in the return_value -> delete from there + */ } } + } else if (behavior == DIFF_KEY) { + /* + the behavior here differs from INTERSECT_KEY in php_intersect + since in the "diff" case we have to remove the entry from + return_value while when doing intersection the entry must not + be deleted. + */ + break; /* remove the key */ } } } @@ -3337,7 +3389,7 @@ static void php_array_diff(INTERNAL_FUNCTION_PARAMETERS, int behavior, int data_ if (diff_data_compare_func(ptrs[0] - 1, ptrs[0] TSRMLS_CC)) { break; } - } else if (behavior == DIFF_ASSOC) { + } else if (behavior == DIFF_ASSOC || behavior == DIFF_KEY) { /* in this case no array_key_compare is needed */ break; } @@ -3353,7 +3405,7 @@ static void php_array_diff(INTERNAL_FUNCTION_PARAMETERS, int behavior, int data_ if (diff_data_compare_func(ptrs[0]-1, ptrs[0] TSRMLS_CC)) { break; } - } else if (behavior == DIFF_ASSOC) { + } else if (behavior == DIFF_ASSOC || behavior == DIFF_KEY) { /* in this case no array_key_compare is needed */ break; } @@ -3374,6 +3426,25 @@ out: } +/* {{{ proto array array_diff_key(array arr1, array arr2 [, array ...]) + Returns the entries of arr1 that have keys which are not present in any of the others arguments. This function is like array_diff() but works on the keys instead of the values. The associativity is preserved. */ +PHP_FUNCTION(array_diff_key) +{ + php_array_diff(INTERNAL_FUNCTION_PARAM_PASSTHRU, DIFF_KEY, + DIFF_COMP_DATA_INTERNAL, DIFF_COMP_KEY_INTERNAL); +} +/* }}} */ + +/* {{{ proto array array_diff_ukey(array arr1, array arr2 [, array ...], callback key_comp_func) + Returns the entries of arr1 that have keys which are not present in any of the others arguments. User supplied function is used for comparing the keys. This function is like array_udiff() but works on the keys instead of the values. The associativity is preserved. */ +PHP_FUNCTION(array_diff_ukey) +{ + php_array_diff(INTERNAL_FUNCTION_PARAM_PASSTHRU, DIFF_KEY, + DIFF_COMP_DATA_INTERNAL, DIFF_COMP_KEY_USER); +} +/* }}} */ + + /* {{{ proto array array_diff(array arr1, array arr2 [, array ...]) Returns the entries of arr1 that have values which are not present in any of the others arguments. */ PHP_FUNCTION(array_diff) diff --git a/ext/standard/basic_functions.c b/ext/standard/basic_functions.c index 8d2546c863..7817f1e0b3 100644 --- a/ext/standard/basic_functions.c +++ b/ext/standard/basic_functions.c @@ -779,12 +779,16 @@ function_entry basic_functions[] = { PHP_FE(array_rand, NULL) PHP_FE(array_unique, NULL) PHP_FE(array_intersect, NULL) + PHP_FE(array_intersect_key, NULL) + PHP_FE(array_intersect_ukey, NULL) PHP_FE(array_uintersect, NULL) PHP_FE(array_intersect_assoc, NULL) PHP_FE(array_uintersect_assoc, NULL) PHP_FE(array_intersect_uassoc, NULL) PHP_FE(array_uintersect_uassoc, NULL) PHP_FE(array_diff, NULL) + PHP_FE(array_diff_key, NULL) + PHP_FE(array_diff_ukey, NULL) PHP_FE(array_udiff, NULL) PHP_FE(array_diff_assoc, NULL) PHP_FE(array_udiff_assoc, NULL) @@ -794,8 +798,8 @@ function_entry basic_functions[] = { PHP_FE(array_filter, NULL) PHP_FE(array_map, NULL) PHP_FE(array_chunk, NULL) - PHP_FE(array_combine, NULL) - PHP_FE(array_key_exists, NULL) + PHP_FE(array_combine, NULL) + PHP_FE(array_key_exists, NULL) /* aliases from array.c */ PHP_FALIAS(pos, current, first_arg_force_ref) diff --git a/ext/standard/php_array.h b/ext/standard/php_array.h index 65a8394601..077862c5c3 100644 --- a/ext/standard/php_array.h +++ b/ext/standard/php_array.h @@ -76,12 +76,16 @@ PHP_FUNCTION(array_change_key_case); PHP_FUNCTION(array_rand); PHP_FUNCTION(array_unique); PHP_FUNCTION(array_intersect); +PHP_FUNCTION(array_intersect_key); +PHP_FUNCTION(array_intersect_ukey); PHP_FUNCTION(array_uintersect); PHP_FUNCTION(array_intersect_assoc); PHP_FUNCTION(array_uintersect_assoc); PHP_FUNCTION(array_intersect_uassoc); PHP_FUNCTION(array_uintersect_uassoc); PHP_FUNCTION(array_diff); +PHP_FUNCTION(array_diff_key); +PHP_FUNCTION(array_diff_ukey); PHP_FUNCTION(array_udiff); PHP_FUNCTION(array_diff_assoc); PHP_FUNCTION(array_udiff_assoc); diff --git a/ext/standard/tests/array/array_diff_key.phpt b/ext/standard/tests/array/array_diff_key.phpt new file mode 100644 index 0000000000..91664a8711 --- /dev/null +++ b/ext/standard/tests/array/array_diff_key.phpt @@ -0,0 +1,250 @@ +--TEST-- +Test of the array_diff_key() and array_diff_ukey() +--FILE-- + &$a_f_el) { $a_f_el =$k*2;} +foreach ($b_f as $k=> &$b_f_el) { $b_f_el =$k*2;} +foreach ($c_f as $k=> &$c_f_el) { $c_f_el =$k*2;} +foreach ($d_f as $k=> &$d_f_el) { $d_f_el =$k*2;} + +echo "------ Test $i --------\n";$i++;// 1 +var_dump(array_diff_key($a_f, $b_f));// keys -> 1, 6, 15, 1200 +var_dump(array_diff_ukey($a_f, $b_f, "comp_func"));// 1, 6, 15, 1200 + +echo "------ Test $i --------\n";$i++;// 2 +var_dump(array_diff_key($a_f, $c_f));// keys -> 1, 15, 1200 +var_dump(array_diff_ukey($a_f, $c_f, "comp_func"));// 1, 15, 1200 + +echo "------ Test $i --------\n";$i++;// 3 +var_dump(array_diff_key($a_f, $d_f));// 1, 6, 2, 15, 1200, -2500 +var_dump(array_diff_ukey($a_f, $d_f, "comp_func"));// 1, 6, 2, 15, 1200, -2500 + +echo "------ Test $i --------\n";$i++;// 4 +var_dump(array_diff_key($a_f, $b_f, $c_f));// 1, 15, 1200 +var_dump(array_diff_ukey($a_f, $b_f, $c_f, "comp_func"));// 1, 15, 1200 + +echo "------ Test $i --------\n";$i++;// 5 +var_dump(array_diff_key($a_f, $b_f, $d_f));// 1, 6, 15, 1200 +var_dump(array_diff_ukey($a_f, $b_f, $d_f, "comp_func"));// 1, 6, 15, 1200 + + +echo "------ Test $i --------\n";$i++;// 6 +var_dump(array_diff_key($a_f, $b_f, $c_f, $d_f));// 1, 15, 1200 +var_dump(array_diff_ukey($a_f, $b_f, $c_f, $d_f, "comp_func"));//1, 15, 1200 + + +echo "------ Test $i --------\n";$i++;// 7 +var_dump(array_diff_key($b_f, $c_f));// 7, 11, 1100 +var_dump(array_diff_ukey($b_f, $c_f, "comp_func"));//7, 11, 1100 + +echo "------ Test $i --------\n";$i++;// 8 +var_dump(array_diff_key($b_f, $d_f));//0, 7, 2, 11, 1100, -2500 +var_dump(array_diff_ukey($b_f, $d_f, "comp_func"));//0, 7, 2, 11, 1100, -2500 + + +echo "------ Test $i --------\n";$i++;// 9 +var_dump(array_diff_key($b_f, $c_f, $d_f));// 7, 11, 1100 +var_dump(array_diff_ukey($b_f, $c_f, $d_f, "comp_func"));// 7, 11, 1000 + +function comp_func($a, $b) { + if ($a === $b) return 0; + return ($a > $b)? 1:-1; + +} +?> +--EXPECTF-- +------ Test 1 -------- +array(4) { + [1]=> + &int(2) + [6]=> + &int(12) + [15]=> + &int(30) + [1200]=> + &int(2400) +} +array(4) { + [1]=> + int(2) + [6]=> + int(12) + [15]=> + int(30) + [1200]=> + int(2400) +} +------ Test 2 -------- +array(3) { + [1]=> + int(2) + [15]=> + int(30) + [1200]=> + int(2400) +} +array(3) { + [1]=> + int(2) + [15]=> + int(30) + [1200]=> + int(2400) +} +------ Test 3 -------- +array(6) { + [1]=> + int(2) + [6]=> + int(12) + [2]=> + int(4) + [15]=> + int(30) + [1200]=> + int(2400) + [-2500]=> + &int(-5000) +} +array(6) { + [1]=> + int(2) + [6]=> + int(12) + [2]=> + int(4) + [15]=> + int(30) + [1200]=> + int(2400) + [-2500]=> + &int(-5000) +} +------ Test 4 -------- +array(3) { + [1]=> + int(2) + [15]=> + int(30) + [1200]=> + int(2400) +} +array(3) { + [1]=> + int(2) + [15]=> + int(30) + [1200]=> + int(2400) +} +------ Test 5 -------- +array(4) { + [1]=> + int(2) + [6]=> + int(12) + [15]=> + int(30) + [1200]=> + int(2400) +} +array(4) { + [1]=> + int(2) + [6]=> + int(12) + [15]=> + int(30) + [1200]=> + int(2400) +} +------ Test 6 -------- +array(3) { + [1]=> + int(2) + [15]=> + int(30) + [1200]=> + int(2400) +} +array(3) { + [1]=> + int(2) + [15]=> + int(30) + [1200]=> + int(2400) +} +------ Test 7 -------- +array(3) { + [7]=> + &int(14) + [11]=> + &int(22) + [1100]=> + &int(2200) +} +array(3) { + [7]=> + int(14) + [11]=> + int(22) + [1100]=> + int(2200) +} +------ Test 8 -------- +array(6) { + [0]=> + int(0) + [7]=> + int(14) + [2]=> + int(4) + [11]=> + int(22) + [1100]=> + int(2200) + [-2500]=> + &int(-5000) +} +array(6) { + [0]=> + int(0) + [7]=> + int(14) + [2]=> + int(4) + [11]=> + int(22) + [1100]=> + int(2200) + [-2500]=> + &int(-5000) +} +------ Test 9 -------- +array(3) { + [7]=> + int(14) + [11]=> + int(22) + [1100]=> + int(2200) +} +array(3) { + [7]=> + int(14) + [11]=> + int(22) + [1100]=> + int(2200) +} diff --git a/ext/standard/tests/array/array_intersect_key.phpt b/ext/standard/tests/array/array_intersect_key.phpt new file mode 100644 index 0000000000..6a5965ba73 --- /dev/null +++ b/ext/standard/tests/array/array_intersect_key.phpt @@ -0,0 +1,209 @@ +--TEST-- +Test of the array_intersect_key() and array_intersect_ukey() +--FILE-- + &$a_f_el) { $a_f_el =$k*2;} +foreach ($b_f as $k=> &$b_f_el) { $b_f_el =$k*2;} +foreach ($c_f as $k=> &$c_f_el) { $c_f_el =$k*2;} +foreach ($d_f as $k=> &$d_f_el) { $d_f_el =$k*2;} + +var_dump(array_intersect_key($a_f, $b_f));// keys -> 2, -20, -2500 +var_dump(array_intersect_ukey($a_f, $b_f, "comp_func"));// 2, 20, -2500 +var_dump(array_intersect_key($a_f, $c_f));// keys -> 6, 2, -20, -2500 +var_dump(array_intersect_ukey($a_f, $c_f, "comp_func"));// 6, 2, -20, -2500 +var_dump(array_intersect_key($a_f, $d_f));// -20 +var_dump(array_intersect_ukey($a_f, $d_f, "comp_func"));// -20 + +var_dump(array_intersect_key($a_f, $b_f, $c_f));// 2, -20, -2500 +var_dump(array_intersect_ukey($a_f, $b_f, $c_f, "comp_func"));// 2, -20, -2500 +var_dump(array_intersect_key($a_f, $b_f, $d_f));// -20 +var_dump(array_intersect_ukey($a_f, $b_f, $d_f, "comp_func"));// -20 + +var_dump(array_intersect_key($a_f, $b_f, $c_f, $d_f));// -20 +var_dump(array_intersect_ukey($a_f, $b_f, $c_f, $d_f, "comp_func"));//-20 + + +var_dump(array_intersect_key($b_f, $c_f));// 0, 2, -20, -2500 +var_dump(array_intersect_ukey($b_f, $c_f, "comp_func"));//0, 2, -20, 2500 + +var_dump(array_intersect_key($b_f, $d_f));// -20 +var_dump(array_intersect_ukey($b_f, $d_f, "comp_func"));// -20 + +var_dump(array_intersect_key($b_f, $c_f, $d_f));// -20 +var_dump(array_intersect_ukey($b_f, $c_f, $d_f, "comp_func"));// -20 + + +echo "----- Now testing array_intersect() ------- \n"; +var_dump(array_intersect($a, $b_f)); +var_dump(array_uintersect($a, $b, "comp_func")); +var_dump(array_intersect($a, $b, $c)); +var_dump(array_uintersect($a, $b, $c, "comp_func")); +var_dump(array_intersect($a, $b, $c, $d)); +var_dump(array_uintersect($a, $b, $c, $d, "comp_func")); + +/////////////////////////////////////////////////////////////////////// +function comp_func($a, $b) { + if ($a === $b) return 0; + return ($a > $b)? 1:-1; + +} +?> +--EXPECTF-- +array(3) { + [2]=> + &int(4) + [-20]=> + &int(-40) + [-2500]=> + &int(-5000) +} +array(3) { + [2]=> + int(4) + [-20]=> + int(-40) + [-2500]=> + &int(-5000) +} +array(4) { + [6]=> + int(12) + [2]=> + int(4) + [-20]=> + int(-40) + [-2500]=> + &int(-5000) +} +array(4) { + [6]=> + int(12) + [2]=> + int(4) + [-20]=> + int(-40) + [-2500]=> + &int(-5000) +} +array(1) { + [-20]=> + int(-40) +} +array(1) { + [-20]=> + int(-40) +} +array(3) { + [2]=> + int(4) + [-20]=> + int(-40) + [-2500]=> + &int(-5000) +} +array(3) { + [2]=> + int(4) + [-20]=> + int(-40) + [-2500]=> + &int(-5000) +} +array(1) { + [-20]=> + int(-40) +} +array(1) { + [-20]=> + int(-40) +} +array(1) { + [-20]=> + int(-40) +} +array(1) { + [-20]=> + int(-40) +} +array(4) { + [0]=> + &int(0) + [2]=> + &int(4) + [-20]=> + &int(-40) + [-2500]=> + &int(-5000) +} +array(4) { + [0]=> + int(0) + [2]=> + int(4) + [-20]=> + int(-40) + [-2500]=> + &int(-5000) +} +array(1) { + [-20]=> + int(-40) +} +array(1) { + [-20]=> + int(-40) +} +array(1) { + [-20]=> + int(-40) +} +array(1) { + [-20]=> + int(-40) +} +----- Now testing array_intersect() ------- +array(0) { +} +array(3) { + [2]=> + int(2) + [3]=> + int(-20) + [6]=> + int(-2500) +} +array(3) { + [2]=> + int(2) + [3]=> + int(-20) + [6]=> + int(-2500) +} +array(3) { + [2]=> + int(2) + [3]=> + int(-20) + [6]=> + int(-2500) +} +array(1) { + [3]=> + int(-20) +} +array(1) { + [3]=> + int(-20) +}