]> granicus.if.org Git - php/commitdiff
Allow array_merge() / array_merge_recursive() without arguments
authorDik Takken <d.h.j.takken@xs4all.nl>
Thu, 16 May 2019 20:08:08 +0000 (22:08 +0200)
committerNikita Popov <nikita.ppv@gmail.com>
Tue, 28 May 2019 09:14:15 +0000 (11:14 +0200)
This allows writing

    array_merge(...$arrays)

instead of

    array_merge([], ...$arrays)

and is in line with similar changes to array_push() and array_unshift()
in PHP 7.3.

Closes GH-4175.

UPGRADING
ext/standard/array.c
ext/standard/basic_functions.c
ext/standard/tests/array/array_merge.phpt
ext/standard/tests/array/array_merge_recursive_basic1.phpt
ext/standard/tests/array/array_merge_recursive_error.phpt [deleted file]

index 058a5d0875ad743a9911eb07f114d93d02308ffd..e9201f24d49f41f61278da6430c9c5ee093ac760 100644 (file)
--- a/UPGRADING
+++ b/UPGRADING
@@ -259,6 +259,10 @@ PHP 7.4 UPGRADE NOTES
 
     RFC: https://wiki.php.net/rfc/custom_object_serialization
 
+  . array_merge() and array_merge_recursive() may now be called without any
+    arguments, in which case they will return an empty array. This is useful
+    in conjunction with the spread operator, e.g. array_merge(...$arrays).
+
 ========================================
 3. Changes in SAPI modules
 ========================================
index 34f1284384b72de0716c0c8f72f953f5b0f22921..652a6598770c24c7434bf0ae7879aabdf8c4ab9f 100644 (file)
@@ -3789,10 +3789,14 @@ static zend_always_inline void php_array_merge_wrapper(INTERNAL_FUNCTION_PARAMET
        HashTable *src, *dest;
        uint32_t count = 0;
 
-       ZEND_PARSE_PARAMETERS_START(1, -1)
+       ZEND_PARSE_PARAMETERS_START(0, -1)
                Z_PARAM_VARIADIC('+', args, argc)
        ZEND_PARSE_PARAMETERS_END();
 
+       if (argc == 0) {
+               RETURN_EMPTY_ARRAY();
+       }
+
        for (i = 0; i < argc; i++) {
                zval *arg = args + i;
 
@@ -3882,7 +3886,7 @@ static zend_always_inline void php_array_merge_wrapper(INTERNAL_FUNCTION_PARAMET
 }
 /* }}} */
 
-/* {{{ proto array array_merge(array arr1 [, array ...])
+/* {{{ proto array array_merge([array ...])
    Merges elements from passed arrays into one array */
 PHP_FUNCTION(array_merge)
 {
@@ -3890,7 +3894,7 @@ PHP_FUNCTION(array_merge)
 }
 /* }}} */
 
-/* {{{ proto array array_merge_recursive(array arr1 [, array ...])
+/* {{{ proto array array_merge_recursive([array ...])
    Recursively merges elements from passed arrays into one array */
 PHP_FUNCTION(array_merge_recursive)
 {
index 0ec4fb39b13a3adb08d1877e17582432903bfa03..920974fe48acaf62a0d0e6aae39770500547f320 100644 (file)
@@ -379,13 +379,11 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_array_slice, 0, 0, 2)
        ZEND_ARG_INFO(0, preserve_keys)
 ZEND_END_ARG_INFO()
 
-ZEND_BEGIN_ARG_INFO_EX(arginfo_array_merge, 0, 0, 1)
-       ZEND_ARG_INFO(0, arr1) /* ARRAY_INFO(0, arg, 0) */
+ZEND_BEGIN_ARG_INFO_EX(arginfo_array_merge, 0, 0, 0)
        ZEND_ARG_VARIADIC_INFO(0, arrays)
 ZEND_END_ARG_INFO()
 
-ZEND_BEGIN_ARG_INFO_EX(arginfo_array_merge_recursive, 0, 0, 1)
-       ZEND_ARG_INFO(0, arr1) /* ARRAY_INFO(0, arg, 0) */
+ZEND_BEGIN_ARG_INFO_EX(arginfo_array_merge_recursive, 0, 0, 0)
        ZEND_ARG_VARIADIC_INFO(0, arrays)
 ZEND_END_ARG_INFO()
 
index e6a8096282c18a5442a55e804fab637cd84542a2..0afc2084af01db07952807978224d1441f37a167 100644 (file)
@@ -80,11 +80,13 @@ var_dump(array_merge($begin_array[4], (array)"type1", (array)10, (array)12.34));
 
 echo "\n*** Testing error conditions ***";
 /* Invalid arguments */
-var_dump(array_merge());
 var_dump(array_merge(100, 200));
 var_dump(array_merge($begin_array[0], $begin_array[1], 100));
 var_dump(array_merge($begin_array[0], $begin_array[1], $arr4));
 
+echo "\n*** Testing array_merge without any arguments ***\n";
+var_dump(array_merge());
+
 echo "Done\n";
 ?>
 --EXPECTF--
@@ -746,9 +748,6 @@ array(7) {
 }
 
 *** Testing error conditions ***
-Warning: array_merge() expects at least 1 parameter, 0 given in %s on line %d
-NULL
-
 Warning: array_merge(): Expected parameter 1 to be an array, int given in %s on line %d
 NULL
 
@@ -759,4 +758,8 @@ Notice: Undefined variable: arr4 in %s on line %d
 
 Warning: array_merge(): Expected parameter 3 to be an array, null given in %s on line %d
 NULL
+
+*** Testing array_merge without any arguments ***
+array(0) {
+}
 Done
index efda3d531b185842b602d1718fd89d3448677b3f..b163a847dbdba57afee5ba1888051c73ea3a717a 100644 (file)
@@ -14,6 +14,10 @@ $arr1 = array(1, array(1, 2));
 $arr2 = array(3, array("hello", 'world'));
 $arr3 = array(array(6, 7), array("str1", 'str2'));
 
+// Calling array_merge_recursive() without arguments
+echo "-- Without arguments --\n";
+var_dump( array_merge_recursive() );
+
 // Calling array_merge_recursive() with default arguments
 echo "-- With default argument --\n";
 var_dump( array_merge_recursive($arr1) );
@@ -27,6 +31,9 @@ echo "Done";
 ?>
 --EXPECT--
 *** Testing array_merge_recursive() : array with default keys ***
+-- Without arguments --
+array(0) {
+}
 -- With default argument --
 array(2) {
   [0]=>
diff --git a/ext/standard/tests/array/array_merge_recursive_error.phpt b/ext/standard/tests/array/array_merge_recursive_error.phpt
deleted file mode 100644 (file)
index ffa9cc9..0000000
+++ /dev/null
@@ -1,25 +0,0 @@
---TEST--
-Test array_merge_recursive() function : error conditions
---FILE--
-<?php
-/* Prototype  : array array_merge_recursive(array $arr1[, array $...])
- * Description: Recursively merges elements from passed arrays into one array
- * Source code: ext/standard/array.c
- */
-
-echo "*** Testing array_merge_recursive() : error conditions ***\n";
-
-// Zero arguments
-echo "\n-- Testing array_merge_recursive() function with Zero arguments --\n";
-var_dump( array_merge_recursive() );
-
-echo "Done";
-?>
---EXPECTF--
-*** Testing array_merge_recursive() : error conditions ***
-
--- Testing array_merge_recursive() function with Zero arguments --
-
-Warning: array_merge_recursive() expects at least 1 parameter, 0 given in %s on line %d
-NULL
-Done