Difference between array_merge() and array_combine() functions in PHP
Last Updated :
30 Sep, 2021
array_merge() Function: The array_merge() function is used to merge two or more arrays into a single array. This function is used to merge the elements or values of two or more arrays together into a single array. The merging occurs in such a manner that the values of one array are appended at the end of the previous array. The function takes the list of arrays separated by commas as a parameter that is needed to be merged and returns a new array with merged values of arrays passed in parameter.
Syntax:
array array_merge( $array1, $array2, ...., $array n)
where, $array1, $array2, . . . are the input arrays that need to be merged.
Example: PHP program to merge two arrays.
PHP
<?php
// Define array1 with keys and values
$array1 = array("subject1" => "Python","subject2" => "sql");
// Define array2 with keys and values
$array2 = array("subject3" => "c/c++","subject4" => "java");
// Merge both array1 and array2
$final = array_merge($array1, $array2);
// Display merged array
print_r($final);
?>
OutputArray
(
[subject1] => Python
[subject2] => sql
[subject3] => c/c++
[subject4] => java
)
Example 2: PHP program to merge multiple arrays.
PHP
<?php
// Define array1 with keys and values
$array1 = array("subject1" => "Python", "subject2" => "sql");
// Define array2 with keys and values
$array2 = array("subject3" => "c/c++", "subject4" => "java");
// Define array3 with keys and values
$array3 = array("subject5" => "CN", "subject6" => "OS");
// Define array4 with keys and values
$array4 = array("subject7" => "data mining", "subject8" => "C#");
// Merge all arrays
$final = array_merge($array1, $array2, $array3, $array4);
// Display merged array
print_r($final);
?>
OutputArray
(
[subject1] => Python
[subject2] => sql
[subject3] => c/c++
[subject4] => java
[subject5] => CN
[subject6] => OS
[subject7] => data mining
[subject8] => C#
)
array_combine() Function: The array_combine() function is used to combine two arrays and create a new array by using one array for keys and another array for values i.e. all elements of one array will be the keys of new array and all elements of the second array will be the values of this new array.
Syntax:
array_combine(array1, array2)
Where, array1 is the first array with keys and array2 is the second array with the values.
Example: PHP program to combine arrays.
PHP
<?php
// Define array1 with keys
$array1 = array("subject1" ,"subject2");
// Define array2 with values
$array2 = array( "c/c++", "java");
// Combine two arrays
$final = array_combine($array1, $array2);
// Display merged array
print_r($final);
?>
OutputArray
(
[subject1] => c/c++
[subject2] => java
)
Example 2:
PHP
<?php
// Define array1 with keys
$array1 = array("subject1", "subject2", "subject3", "subject4");
// Define array2 with values
$array2 = array( "c/c++", "java", "Python", "HTML");
// Combine two arrays
$final = array_combine($array1, $array2);
// Display merged array
print_r($final);
?>
OutputArray
(
[subject1] => c/c++
[subject2] => java
[subject3] => Python
[subject4] => HTML
)
Difference Between array_merge() and array_combine() Function:
array_merge() Function
| array_combine() Function
|
This function merges the two or more arrays. | This array combine only two arrays. |
This function merges the arrays such that all the arrays have keys and values. | This function combine the one array containing keys and another array containing values. |
The arrays are appended at the end of the first array. | The arrays are combined. |
Similar Reads
What is the differences between array_merge() and array_merge_recursive() functions in PHP ? In this article, we will see the array_merge() and array_merge_recursive() functions, along with understanding their basic implementation, & the differences between them. Both the array_merge() Function and array_merge_recursive() function can be used to combine multiple arrays into a single arr
3 min read
How to use array_merge() and array_combine() in PHP ? In this article, we will discuss about how to use array_merge() and array_combine() functions in PHP. Both functions are array based functions used to combine two or more arrays using PHP. We will see each function with syntax and implementation array_merge() FunctionThis function merges the two or
3 min read
Difference Between Spread Operator and Array.concat() in Typescript The spread operator and concat() method both are used to add elements to an array. But they are different from each other in many ways. Let us discuss the difference between both of them in detail. Spread OperatorThe spread operator creates a new array by merging the elements of the passed arrays or
3 min read
What are the differences between array_map(), array_walk() and array_filter() methods in PHP ? array_map() Method: The array_map() is used to modify all elements in one or more arrays according to some user-defined condition in an easy manner. It basically sends each of the elements of an array to a user-defined function and returns an array with new values as modified by that function. Synta
5 min read
PHP | Deleting an element from array using array_diff() Given an array, we have to delete an element from that array using the array_diff() function in PHP.Examples:Input : $arr = array(2, 8, 9, 7, 6, 5); $arr = array_diff($arr, array(9)); Output : Array ( [0] => 2 [1] => 8 [3] => 7 [4] => 6 [5] => 5 ) Input : $arr = array("shubham", "aksh
2 min read