Backend Development 2 min read

PHP array_merge() Function: Merging One or More Arrays

The article explains PHP's array_merge() function, detailing how it combines one or multiple arrays, the handling of string and numeric keys, parameter descriptions, return values, and provides a complete code example with expected output.

Laravel Tech Community
Laravel Tech Community
Laravel Tech Community
PHP array_merge() Function: Merging One or More Arrays

array_merge() merges one or more arrays by appending the values of later arrays to the previous ones and returns the resulting array.

If the arrays have identical string keys, later values overwrite earlier ones; numeric keys are reindexed and values are appended.

When only a single numeric-indexed array is provided, the keys are reindexed sequentially.

Parameters

array1 – the initial array to merge.

... – additional arrays to merge recursively.

Return value

Returns the merged array.

Example

"red", 2, 4);
$array2 = array("a", "color"=>"green", "shape"=>"trapezoid", 4);
$result = array_merge($array1, $array2);
print_r($result);
?>

Output

Array
(
    [color] => green
    [0] => 2
    [1] => 4
    [2] => a
    [shape] => trapezoid
    [3] => 4
)
backendProgrammingPHPArraytutorialarray_merge
Laravel Tech Community
Written by

Laravel Tech Community

Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.