Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

How to use array_merge() and array_combine() in PHP ?

  • Last Updated : 07 Oct, 2021

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() Function: This function merges the two or more arrays  such that all the arrays have keys and values. The arrays are appended at the end of the first array.

Syntax:

array_merge( array1, array2, ..., array n )

Parameters: Arrays are the input arrays to be merged.

Return type: Single array with merged elements.



Example: PHP example 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);
  
?>
Output
Array
(
    [subject1] => Python
    [subject2] => sql
    [subject3] => c/c++
    [subject4] => java
)

Example 2: 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);
  
?>
Output
Array
(
    [subject1] => Python
    [subject2] => sql
    [subject3] => c/c++
    [subject4] => java
    [subject5] => CN
    [subject6] => OS
    [subject7] => data mining
    [subject8] => C#
)

array_combine() Function: This function combine only two arrays with one array containing keys and another array containing values.

Syntax:

array_combine(array1, array2)

Parameters:

  • array1 is the first array with keys.
  • array2 is the second array with values.

Return Value: It returns the combined array.

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);
  
?>
Output
Array
(
    [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);
  
?>
Output
Array
(
    [subject1] => c/c++
    [subject2] => java
    [subject3] => Python
    [subject4] => HTML
)



My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!