SyntaxStudy
Sign Up
Home PHP Reference array_merge()

array_merge()

function

Merges arrays. String keys from later arrays overwrite; numeric keys are re-indexed.

Syntax

array_merge(array ...$arrays): array

Example

php
<?php
$defaults = ['color'=>'blue','size'=>'medium'];
$custom   = ['color'=>'red','qty'=>5];
$merged = array_merge($defaults, $custom);
// ['color'=>'red','size'=>'medium','qty'=>5]

// Spread operator (PHP 8.1+)
$merged2 = [...$defaults, ...$custom];