Skip to content

Commit 77cf3d7

Browse files
Dik Takkennikic
authored andcommitted
Allow array_merge() / array_merge_recursive() without arguments
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.
1 parent 072f289 commit 77cf3d7

File tree

6 files changed

+27
-36
lines changed

6 files changed

+27
-36
lines changed

UPGRADING

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,10 @@ PHP 7.4 UPGRADE NOTES
259259

260260
RFC: https://wiki.php.net/rfc/custom_object_serialization
261261

262+
. array_merge() and array_merge_recursive() may now be called without any
263+
arguments, in which case they will return an empty array. This is useful
264+
in conjunction with the spread operator, e.g. array_merge(...$arrays).
265+
262266
========================================
263267
3. Changes in SAPI modules
264268
========================================

ext/standard/array.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3789,10 +3789,14 @@ static zend_always_inline void php_array_merge_wrapper(INTERNAL_FUNCTION_PARAMET
37893789
HashTable *src, *dest;
37903790
uint32_t count = 0;
37913791

3792-
ZEND_PARSE_PARAMETERS_START(1, -1)
3792+
ZEND_PARSE_PARAMETERS_START(0, -1)
37933793
Z_PARAM_VARIADIC('+', args, argc)
37943794
ZEND_PARSE_PARAMETERS_END();
37953795

3796+
if (argc == 0) {
3797+
RETURN_EMPTY_ARRAY();
3798+
}
3799+
37963800
for (i = 0; i < argc; i++) {
37973801
zval *arg = args + i;
37983802

@@ -3882,15 +3886,15 @@ static zend_always_inline void php_array_merge_wrapper(INTERNAL_FUNCTION_PARAMET
38823886
}
38833887
/* }}} */
38843888

3885-
/* {{{ proto array array_merge(array arr1 [, array ...])
3889+
/* {{{ proto array array_merge([array ...])
38863890
Merges elements from passed arrays into one array */
38873891
PHP_FUNCTION(array_merge)
38883892
{
38893893
php_array_merge_wrapper(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
38903894
}
38913895
/* }}} */
38923896

3893-
/* {{{ proto array array_merge_recursive(array arr1 [, array ...])
3897+
/* {{{ proto array array_merge_recursive([array ...])
38943898
Recursively merges elements from passed arrays into one array */
38953899
PHP_FUNCTION(array_merge_recursive)
38963900
{

ext/standard/basic_functions.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -379,13 +379,11 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_array_slice, 0, 0, 2)
379379
ZEND_ARG_INFO(0, preserve_keys)
380380
ZEND_END_ARG_INFO()
381381

382-
ZEND_BEGIN_ARG_INFO_EX(arginfo_array_merge, 0, 0, 1)
383-
ZEND_ARG_INFO(0, arr1) /* ARRAY_INFO(0, arg, 0) */
382+
ZEND_BEGIN_ARG_INFO_EX(arginfo_array_merge, 0, 0, 0)
384383
ZEND_ARG_VARIADIC_INFO(0, arrays)
385384
ZEND_END_ARG_INFO()
386385

387-
ZEND_BEGIN_ARG_INFO_EX(arginfo_array_merge_recursive, 0, 0, 1)
388-
ZEND_ARG_INFO(0, arr1) /* ARRAY_INFO(0, arg, 0) */
386+
ZEND_BEGIN_ARG_INFO_EX(arginfo_array_merge_recursive, 0, 0, 0)
389387
ZEND_ARG_VARIADIC_INFO(0, arrays)
390388
ZEND_END_ARG_INFO()
391389

ext/standard/tests/array/array_merge.phpt

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,13 @@ var_dump(array_merge($begin_array[4], (array)"type1", (array)10, (array)12.34));
8080

8181
echo "\n*** Testing error conditions ***";
8282
/* Invalid arguments */
83-
var_dump(array_merge());
8483
var_dump(array_merge(100, 200));
8584
var_dump(array_merge($begin_array[0], $begin_array[1], 100));
8685
var_dump(array_merge($begin_array[0], $begin_array[1], $arr4));
8786

87+
echo "\n*** Testing array_merge without any arguments ***\n";
88+
var_dump(array_merge());
89+
8890
echo "Done\n";
8991
?>
9092
--EXPECTF--
@@ -746,9 +748,6 @@ array(7) {
746748
}
747749

748750
*** Testing error conditions ***
749-
Warning: array_merge() expects at least 1 parameter, 0 given in %s on line %d
750-
NULL
751-
752751
Warning: array_merge(): Expected parameter 1 to be an array, int given in %s on line %d
753752
NULL
754753

@@ -759,4 +758,8 @@ Notice: Undefined variable: arr4 in %s on line %d
759758

760759
Warning: array_merge(): Expected parameter 3 to be an array, null given in %s on line %d
761760
NULL
761+
762+
*** Testing array_merge without any arguments ***
763+
array(0) {
764+
}
762765
Done

ext/standard/tests/array/array_merge_recursive_basic1.phpt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ $arr1 = array(1, array(1, 2));
1414
$arr2 = array(3, array("hello", 'world'));
1515
$arr3 = array(array(6, 7), array("str1", 'str2'));
1616

17+
// Calling array_merge_recursive() without arguments
18+
echo "-- Without arguments --\n";
19+
var_dump( array_merge_recursive() );
20+
1721
// Calling array_merge_recursive() with default arguments
1822
echo "-- With default argument --\n";
1923
var_dump( array_merge_recursive($arr1) );
@@ -27,6 +31,9 @@ echo "Done";
2731
?>
2832
--EXPECT--
2933
*** Testing array_merge_recursive() : array with default keys ***
34+
-- Without arguments --
35+
array(0) {
36+
}
3037
-- With default argument --
3138
array(2) {
3239
[0]=>

ext/standard/tests/array/array_merge_recursive_error.phpt

Lines changed: 0 additions & 25 deletions
This file was deleted.

0 commit comments

Comments
 (0)