The array_merge in PHP function combines many arrays into a single one. You may give the function a single array or as many as you like. Notice that if the keys of two or more array elements are the same, the last one takes precedence.

Note: If you pass only one array to the array merge() function with integer keys, the function returns a new array_merge in PHP with integer keys starting at 0 and increasing by 1 for each value 

When two or more array elements share the same key, this function differs from the array_merge_recursive() function. Rather than overriding the keys, the array_merge_recursive() function creates an array from the value.

The array_merge in PHP is a built-in function that combines two or more arrays into a single array. This function combines the elements or values from two or more arrays into a single array. The values of one array are appended to the end of the previous array during the merging process. The function takes as a parameter a list of arrays that need to be combined, separated by commas, and returns a new array with the merged values of the arrays passed in parameter.

If the string keys in both input arrays are the same, the later value for that key would overwrite the previous one. If the arrays contain numeric keys, the later value will be appended to the original value rather than overwriting it.

In the result list, values in the input arrays with numeric keys will be renumbered with incrementing keys starting at zero.

Post Graduate Program: Full Stack Web Development

in Collaboration with Caltech CTMEEnroll Now
Post Graduate Program: Full Stack Web Development

Syntax:

array array_merge($array1, $array2, ......, $arrayn)

Parameter Values

  • $array1: A required field that accepts an array

  • $array2: An optional field that accepts an array

  • $array3: An optional field that accepts an array

Array_merge_in_PHP_1.

The array_merge in PHP function takes as a parameter, a list of arrays separated by commas that need to be merged, as seen in the syntax. In the syntax, there are n arrays (($array1, $array2,......, $arrayn) separated by (‘,'). Any number of arrays can be passed as a parameter.

Return Value: It returns a new array containing the elements of all arrays passed in as parameters, with the values of one array appended to the end of the previous array.

The array merge() feature in PHP is demonstrated in the following programs:

Example

<?php

$array1 = array("color" => "red", 2, 4);

$array2 = array("a", "b", "color" => "green", "shape" => "trapezoid", 4);

$result = array_merge($array1, $array2);

print_r($result);

?> 

Output:

Array_merge_in_PHP_2.

Merging Two Simple Arrays

When the array_merge in PHP function is called for two more arrays, the values of one array are appended to the end of the previous array. When two elements have the same string key, the latter value takes precedence. Starting with zero, the integer keys will be renumbered. The array merge() function can be used to combine two arrays in the following way:

Example

<?PHP

$my_array1 = array("size" => "big", 2,3 );

$my_array2 = array("a", "b", "size" => "medium",

                        "shape" => "circle", 4);

$res = array_merge($my_array1, $my_array2);

print_r($res);

?>

Output

Array_merge_in_PHP_3 

Passing Parameters

If the array merge() function is called with parameters and the array parameter's keys are an integer, the keys in the output array will be renumbered starting at 0 and increasing by 1 for each subsequent variable. The following example demonstrates this:

Example:

<?PHP

$my_array = array(1 => "This", 3=>"is", 2=>"Simplilearn");

$res = array_merge($my_array);

print_r($res); 

?>

Output:

Array_merge_in_PHP_4

Union Operator

The Union operator joins two array_merge in PHP together by appending the right-hand array to the left-hand array. If a key occurs in both arrays, the left-hand array's elements will be used, while the corresponding elements from the right-hand array will be ignored. The define() function is used to define constants in the following example.

Example

<?php

$arr1=array("phy"=>70, "che"=>80, "math"=>90);

$arr2=array("Eng"=>70, "Bio"=>80,"CompSci"=>90);

$arr3=$arr1+$arr2;

var_dump($arr3);

?> 

Output:

Array_merge_in_PHP_5 

New Course: Full Stack Development for Beginners

Learn Git Command, Angular, NodeJS, Maven & MoreEnroll Now
New Course: Full Stack Development for Beginners

Comparison of Arrays

When two arrays have the same key-value pairs, they are said to be identical. An indexed array and an associative array with keys corresponding to the index of elements in the first are used in the following example. As a result, both are equal.

Example

<?php

$arr1=array(0=>70, 2=>80, 1=>90);

$arr2=array(70,90,80);

var_dump ($arr1==$arr2);

var_dump ($arr2!=$arr1);

?>

Output:

Array_merge_in_PHP_6.

Advance your career as a MEAN stack developer with the Full Stack Web Developer - MEAN Stack Master's Program. Enroll now!

Conclusion

The array_merge in PHP function joins two or more arrays together. If the input arrays have matching string keys, the later value will take precedence over its predecessor. If the input array_merge in PHP contains numeric keys, the later value will be appended rather than overwritten. If there is only one array, it is numerically indexed, and the keys are continuously reindexed.

Want to learn the skills of array_merge in PHP for a great career ahead in the industry? Elevate your career by enrolling in the ongoing courses from Simplilearn such as the Full Stack Web Development course. The courses offered by Simplelearn provide complete guidance on the subject and you get the opportunity to learn from experts active in their field, not out-of-touch trainers.

If you feel that you already know about the array_merge in PHP, then why not work towards further enhancing your skills. Check out Skill-Up, a platform  where most in-demand courses are available for free and ensure skill development.

About the Author

SimplilearnSimplilearn

Simplilearn is one of the world’s leading providers of online training for Digital Marketing, Cloud Computing, Project Management, Data Science, IT, Software Development, and many other emerging technologies.

View More
  • Disclaimer
  • PMP, PMI, PMBOK, CAPM, PgMP, PfMP, ACP, PBA, RMP, SP, and OPM3 are registered marks of the Project Management Institute, Inc.
  • *According to Simplilearn survey conducted and subject to terms & conditions with Ernst & Young LLP (EY) as Process Advisors