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
)

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

Similar Reads

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....

array_combine() Function

This function combine only two arrays with one array containing keys and another array containing values....

Difference table of array_merge() and array_combine() in PHP

Feature/Aspectarray_merge()array_combine()PurposeCombines two or more arrays into oneCreates an associative array by combining keys and valuesInputAccepts multiple arraysRequires two arrays: one for keys and one for valuesKey HandlingOverwrites values if keys are duplicated (for associative arrays)Keys from the first array, values from the second arrayOutputReturns a single merged arrayReturns an associative arrayUse CaseMerging indexed or associative arraysCreating an associative array from two separate arrays...