1.2.1266. Unpacking Inside Arrays

The variadic operator is now available inside arrays. Until PHP 7.4, it is not possible to use the variadic operator, or ... inside arrays.

The workaround is to use array_merge(), after checking that arrays are not empty.

<?php

$a = ['a', 'b', 'c'];
$b = ['d', 'e', 'f'];

// PHP 7.4
$c = [...$a, ...$b];

// PHP 7.3 and older
$c = array_merge($a, $b);

?>

See also Spread Operator in Array Expression and PHP 5.6 and the Splat Operator.

1.2.1266.1. Suggestions

  • Replace array_merge() with ....

1.2.1266.2. Specs

Short name

Php/UnpackingInsideArrays

Rulesets

All, CompatibilityPHP53, CompatibilityPHP54, CompatibilityPHP55, CompatibilityPHP56, CompatibilityPHP70, CompatibilityPHP71, CompatibilityPHP72, CompatibilityPHP73

Exakat since

1.8.0

PHP Version

With PHP 7.4 and more recent

Severity

Minor

Time To Fix

Quick (30 mins)

Precision

High

Features

array-spread

Available in

Entreprise Edition, Exakat Cloud