From: Andrei Zmievski Date: Tue, 30 May 2000 17:03:56 +0000 (+0000) Subject: @- Added array_merge_recursive() that will recursively merge values X-Git-Tag: PRE_EIGHT_BYTE_ALLOC_PATCH~180 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=01a4220954065327a20752457993b624deb8ab7b;p=php @- Added array_merge_recursive() that will recursively merge values @ under the same keys. (Andrei) --- diff --git a/ext/standard/array.c b/ext/standard/array.c index 82b282f1c1..fa1c210f83 100644 --- a/ext/standard/array.c +++ b/ext/standard/array.c @@ -1734,17 +1734,48 @@ PHP_FUNCTION(array_slice) /* }}} */ -/* {{{ proto array array_merge(array arr1, array arr2 [, ...]) - Merges elements from passed arrays into one array */ -PHP_FUNCTION(array_merge) +static void php_array_merge_impl(HashTable *dest, HashTable *src, int recursive) +{ + zval **src_entry, + **dest_entry; + char *string_key; + ulong num_key; + + zend_hash_internal_pointer_reset(src); + while(zend_hash_get_current_data(src, (void **)&src_entry) == SUCCESS) { + switch (zend_hash_get_current_key(src, &string_key, &num_key)) { + case HASH_KEY_IS_STRING: + if (recursive && + zend_hash_find(dest, string_key, strlen(string_key) + 1, + (void **)&dest_entry) == SUCCESS) { + convert_to_array_ex(dest_entry); + convert_to_array_ex(src_entry); + php_array_merge_impl(Z_ARRVAL_PP(dest_entry), + Z_ARRVAL_PP(src_entry), recursive); + } else { + (*src_entry)->refcount++; + + zend_hash_update(dest, string_key, strlen(string_key)+1, + src_entry, sizeof(zval *), NULL); + } + efree(string_key); + break; + + case HASH_KEY_IS_LONG: + (*src_entry)->refcount++; + zend_hash_next_index_insert(dest, src_entry, sizeof(zval *), NULL); + break; + } + + zend_hash_move_forward(src); + } +} + +static void php_array_merge(INTERNAL_FUNCTION_PARAMETERS, int recursive) { - zval ***args = NULL, - **entry; - HashTable *hash; + zval ***args = NULL; int argc, i; - char *string_key; - ulong num_key; /* Get the argument count and check it */ argc = ARG_COUNT(ht); @@ -1762,35 +1793,29 @@ PHP_FUNCTION(array_merge) array_init(return_value); for (i=0; itype != IS_ARRAY) { - php_error(E_WARNING, "Skipping argument #%d to array_merge(), since it's not an array", i+1); - continue; - } - hash = (*args[i])->value.ht; - - zend_hash_internal_pointer_reset(hash); - while(zend_hash_get_current_data(hash, (void **)&entry) == SUCCESS) { - (*entry)->refcount++; - - switch (zend_hash_get_current_key(hash, &string_key, &num_key)) { - case HASH_KEY_IS_STRING: - zend_hash_update(return_value->value.ht, string_key, strlen(string_key)+1, - entry, sizeof(zval *), NULL); - efree(string_key); - break; - - case HASH_KEY_IS_LONG: - zend_hash_next_index_insert(return_value->value.ht, - entry, sizeof(zval *), NULL); - break; - } - - zend_hash_move_forward(hash); - } + convert_to_array_ex(args[i]); + php_array_merge_impl(Z_ARRVAL_P(return_value), Z_ARRVAL_PP(args[i]), recursive); } efree(args); } + + +/* {{{ proto array array_merge(array arr1, array arr2 [, ...]) + Merges elements from passed arrays into one array */ +PHP_FUNCTION(array_merge) +{ + php_array_merge(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0); +} +/* }}} */ + + +/* {{{ proto array array_merge(array arr1, array arr2 [, ...]) + Recursively merges elements from passed arrays into one array */ +PHP_FUNCTION(array_merge_recursive) +{ + php_array_merge(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1); +} /* }}} */ diff --git a/ext/standard/basic_functions.c b/ext/standard/basic_functions.c index f233efe6cb..a1f2ac8149 100644 --- a/ext/standard/basic_functions.c +++ b/ext/standard/basic_functions.c @@ -495,6 +495,7 @@ function_entry basic_functions[] = { PHP_FE(array_splice, first_arg_force_ref) PHP_FE(array_slice, NULL) PHP_FE(array_merge, NULL) + PHP_FE(array_merge_recursive, NULL) PHP_FE(array_keys, NULL) PHP_FE(array_values, NULL) PHP_FE(array_count_values, NULL) diff --git a/ext/standard/php_array.h b/ext/standard/php_array.h index d6f073bab6..38b3174c24 100644 --- a/ext/standard/php_array.h +++ b/ext/standard/php_array.h @@ -61,6 +61,7 @@ PHP_FUNCTION(array_unshift); PHP_FUNCTION(array_splice); PHP_FUNCTION(array_slice); PHP_FUNCTION(array_merge); +PHP_FUNCTION(array_merge_recursive); PHP_FUNCTION(array_keys); PHP_FUNCTION(array_values); PHP_FUNCTION(array_count_values);