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.
array array_merge($array1, $array2, ......, $arrayn)
- $array1: A required field that accepts an array
- $array2: An optional field that accepts an array
- $array3: An optional field that accepts an array
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:
$array1 = array("color" => "red", 2, 4);
$array2 = array("a", "b", "color" => "green", "shape" => "trapezoid", 4);
$result = array_merge($array1, $array2);
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:
$my_array1 = array("size" => "big", 2,3 );
$my_array2 = array("a", "b", "size" => "medium",
"shape" => "circle", 4);
$res = array_merge($my_array1, $my_array2);
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:
$my_array = array(1 => "This", 3=>"is", 2=>"Simplilearn");
$res = array_merge($my_array);
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.
$arr1=array("phy"=>70, "che"=>80, "math"=>90);
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.
$arr1=array(0=>70, 2=>80, 1=>90);
Advance your career as a MEAN stack developer with the Full Stack Web Developer - MEAN Stack Master's Program. Enroll now!
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.