, ,

What is the difference between array_merge and array_combine?

Posted by

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
  
// Define array1 with keys and values
$array1 = array("subject1" => "Laravel","subject2" => "Jira");
  
  
// Define array2 with keys and values
$array2 = array("subject3" => "Python","subject4" => "Bootstrap");
  
// Merge both array1 and array2
$final = array_merge($array1, $array2);
  
// Display merged array
print_r($final);
  
?>

Output
Array
(
[subject1] => Laravel
[subject2] => Jira
[subject3] => Python
[subject4] => Bootstrap
)

Example 2: PHP program to merge multiple arrays.

<?php
  
// Define array1 with keys and values
$array1 = array("subject1" => "Laravel", "subject2" => "Jira");
  
  
// Define array2 with keys and values
$array2 = array("subject3" => "Python", "subject4" => "Bootstrap");
  
// Define array3 with keys and values
$array3 = array("subject5" => "Mac", "subject6" => "Linux");
  
// Define array4 with keys and values
$array4 = array("subject7" => "Data capturing", "subject8" => "C++");
  
// Merge all arrays
$final = array_merge($array1, $array2, $array3, $array4);
  
// Display merged array
print_r($final);
  
?>

Output
Array
(
[subject1] => Laravel
[subject2] => Jira
[subject3] => Python
[subject4] => Bootstrap
[subject5] => Mac
[subject6] => Linux
[subject7] => Data capturing
[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
  
// Define array1 with keys 
$array1 = array("subject1" ,"subject2");
  
// Define array2 with  values
$array2 = array( "laravel", "jira");
  
// Combine two arrays
$final = array_combine($array1, $array2);
  
// Display merged array
print_r($final);
  
?>

Output

Array
(
[subject1] => laravel
[subject2] => jira
)

Example 2:

<?php
  
// Define array1 with keys 
$array1 = array("subject1", "subject2", "subject3", "subject4");
  
// Define array2 with values
$array2 = array( "laravel", "jira", "Python", "bootstrap");
  
// Combine two arrays
$final = array_combine($array1, $array2);
  
// Display merged array
print_r($final);
  
?>

Output
Array
(
[subject1] => laravel
[subject2] => jira
[subject3] => Python
[subject4] => bootstarp
)

guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x