1.2.49. Array_merge Needs Array Of Arrays

When collecting data to feed array_merge(), use an array of array as default value. `array(`array()) <https://www.php.net/array>`_` is the neutral value for array_merge();

This analysis also reports when the used types are not an array : array_merge() does not accept scalar values, but only arrays.

Since PHP 7.4, it is possible to call array_merge() without an argument : this means the default value may an empty array.

<?php

// safe default value
$a = array(array());

// when $list is empty, this will trigger an error during array_merge()
foreach($list as $l) {
    $a[] = $l;
}
$b = array_merge(...$a);

?>

See also array_merge.

1.2.49.1. Suggestions

  • Use `array(array())` or `[[]]` as default value for array_merge()

  • Remove any non-array value from the values in the default array

1.2.49.2. Specs

Short name

Structures/ArrayMergeArrayArray

Rulesets

All, Analyze, Changed Behavior

Exakat since

2.1.4

PHP Version

With PHP 7.4 and older

Severity

Minor

Time To Fix

Quick (30 mins)

Precision

High

Features

array

Available in

Entreprise Edition, Exakat Cloud