From a920f00479868db5d3e4821b5f83b4edd97434bd Mon Sep 17 00:00:00 2001 From: Andrey Hristov Date: Sat, 21 Sep 2002 14:50:04 +0000 Subject: [PATCH] New function added - array_intersect_assoc() similar to array_intersect() but the keys are also used in the comparison. So the result is a subset of the result of array_intersect(). Test will be committed too. --- ext/standard/array.c | 66 ++++++++++++++++++++++++++++------ ext/standard/basic_functions.c | 1 + ext/standard/php_array.h | 1 + 3 files changed, 58 insertions(+), 10 deletions(-) diff --git a/ext/standard/array.c b/ext/standard/array.c index 046d65621f..dc27bb70a5 100644 --- a/ext/standard/array.c +++ b/ext/standard/array.c @@ -78,6 +78,9 @@ php_array_globals array_globals; #define DIFF_NORMAL 0 #define DIFF_ASSOC 1 +#define INTERSECT_NORMAL 0 +#define INTERSECT_ASSOC 1 + PHP_MINIT_FUNCTION(array) { #ifdef ZTS @@ -2519,9 +2522,7 @@ PHP_FUNCTION(array_unique) } /* }}} */ -/* {{{ 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) +static void php_array_intersect(INTERNAL_FUNCTION_PARAMETERS, int behavior) { zval ***args = NULL; HashTable *hash; @@ -2558,7 +2559,11 @@ PHP_FUNCTION(array_intersect) for (p = hash->pListHead; p; p = p->pListNext) *list++ = p; *list = NULL; - zend_qsort((void *) lists[i], hash->nNumOfElements, sizeof(Bucket *), array_data_compare TSRMLS_CC); + if (behavior == INTERSECT_NORMAL) { + zend_qsort((void *) lists[i], hash->nNumOfElements, sizeof(Bucket *), array_data_compare TSRMLS_CC); + } else if (behavior == INTERSECT_ASSOC) { + zend_qsort((void *) lists[i], hash->nNumOfElements, sizeof(Bucket *), array_key_compare TSRMLS_CC); + } } /* copy the argument array */ @@ -2568,11 +2573,26 @@ PHP_FUNCTION(array_intersect) /* go through the lists and look for common values */ while (*ptrs[0]) { for (i=1; ih); } } - if (c) + if (c) /* here we get if not all are equal */ break; ptrs[i]++; } @@ -2598,8 +2618,13 @@ PHP_FUNCTION(array_intersect) zend_hash_index_del(Z_ARRVAL_P(return_value), p->h); if (!*++ptrs[0]) goto out; - if (0 <= array_data_compare(ptrs[0], ptrs[i] TSRMLS_CC)) + if (behavior == INTERSECT_NORMAL) { + if (0 <= array_data_compare(ptrs[0], ptrs[i] TSRMLS_CC)) + break; + } else if (behavior == INTERSECT_ASSOC) { + /* no need of looping because indexes are unique */ break; + } } } else { /* ptrs[0] is present in all the arguments */ @@ -2607,8 +2632,13 @@ PHP_FUNCTION(array_intersect) for (;;) { if (!*++ptrs[0]) goto out; - if (array_data_compare(ptrs[0]-1, ptrs[0] TSRMLS_CC)) + if (behavior == INTERSECT_NORMAL) { + if (array_data_compare(ptrs[0]-1, ptrs[0] TSRMLS_CC)) + break; + } else if (behavior == INTERSECT_ASSOC) { + /* no need of looping because indexes are unique */ break; + } } } } @@ -2622,8 +2652,24 @@ out: efree(lists); efree(args); } + +/* {{{ 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) +{ + php_array_intersect(INTERNAL_FUNCTION_PARAM_PASSTHRU, INTERSECT_NORMAL); +} +/* }}} */ + +/* {{{ proto array array_intersect_assoc(array arr1, array arr2 [, array ...]) + Returns the entries of arr1 that have values which are present in all the other arguments. Keys are used to do more restrctive check */ +PHP_FUNCTION(array_intersect_assoc) +{ + php_array_intersect(INTERNAL_FUNCTION_PARAM_PASSTHRU, INTERSECT_ASSOC); +} /* }}} */ + static void php_array_diff(INTERNAL_FUNCTION_PARAMETERS, int behavior) { zval ***args = NULL; diff --git a/ext/standard/basic_functions.c b/ext/standard/basic_functions.c index cd1dbce977..eebd3b603f 100644 --- a/ext/standard/basic_functions.c +++ b/ext/standard/basic_functions.c @@ -797,6 +797,7 @@ function_entry basic_functions[] = { PHP_FE(array_rand, NULL) PHP_FE(array_unique, NULL) PHP_FE(array_intersect, NULL) + PHP_FE(array_intersect_assoc, NULL) PHP_FE(array_diff, NULL) PHP_FE(array_diff_assoc, NULL) PHP_FE(array_sum, NULL) diff --git a/ext/standard/php_array.h b/ext/standard/php_array.h index f52c06535f..85b462dcb9 100644 --- a/ext/standard/php_array.h +++ b/ext/standard/php_array.h @@ -75,6 +75,7 @@ PHP_FUNCTION(array_change_key_case); PHP_FUNCTION(array_rand); PHP_FUNCTION(array_unique); PHP_FUNCTION(array_intersect); +PHP_FUNCTION(array_intersect_assoc); PHP_FUNCTION(array_diff); PHP_FUNCTION(array_diff_assoc); PHP_FUNCTION(array_sum); -- 2.40.0