How to use array_merge() and array_combine() in PHP ?
Last Updated :
14 Jun, 2024
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);
?>
OutputArray
(
[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);
?>
OutputArray
(
[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);
?>
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 table of array_merge() and array_combine() in PHP
Feature/Aspect | array_merge() | array_combine() |
---|
Purpose | Combines two or more arrays into one | Creates an associative array by combining keys and values |
Input | Accepts multiple arrays | Requires two arrays: one for keys and one for values |
Key Handling | Overwrites values if keys are duplicated (for associative arrays) | Keys from the first array, values from the second array |
Output | Returns a single merged array | Returns an associative array |
Use Case | Merging indexed or associative arrays | Creating an associative array from two separate arrays |
Similar Reads
Difference between array_merge() and array_combine() functions in PHP 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 e
3 min read
How to Merge/Combine Arrays using JavaScript? Given two or more arrays, the task is to merge (or combine) arrays to make a single array in JavaScript. The simplest method to merge two or more arrays is by using array.concat() method.Using Array concat() MethodThe contact() method concatenates (joins) two or more arrays. It creates a new array a
2 min read
PHP Merging two or more arrays using array_merge() The array_merge() function in PHP combines two or more arrays into one. It merges the values from the arrays in the order they are passed. If arrays have duplicate string keys, the latter values overwrite earlier ones, while numerical keys are re-indexed sequentially.Syntaxarray array_merge($array1,
2 min read
How to Insert a New Element in an Array in PHP ? In PHP, an array is a type of data structure that allows us to store similar types of data under a single variable. The array is helpful to create a list of elements of similar types, which can be accessed using their index or key.We can insert an element or item in an array using the below function
5 min read
How to find the index of an element in an array using PHP ? In this article, we will discuss how to find the index of an element in an array in PHP. Array indexing starts from 0 to n-1. Here we have some common approachesTable of ContentUsing array_search() FunctionUsing array_flip()Using a custom functionUsing a foreach LoopUsing array_keys FunctionUsing ar
6 min read
How to Find Union and Intersection of Two Unsorted Arrays in PHP? Given two unsorted arrays representing two sets (elements in every array are distinct), find the union and intersection of two arrays in PHP. Example: Input: arr1[] = {7, 1, 5, 2, 3, 6} , arr2[] = {3, 8, 6, 20, 7} Output: Union {1, 2, 3, 5, 6, 7, 8, 20} and Intersection as {3, 6, 7}These are the fol
4 min read
How to display array structure and values in PHP ? In this article, we will discuss how to display the array structure and values in PHP. To display the array structure and its values, we can use var_dump() and print_r() functions. It includes Array sizeArray valuesArray value with IndexEach value data type We will display the array structure using
2 min read
How to get total number of elements used in array in PHP ? In this article, we will discuss how to get total number of elements in PHP from an array. We can get total number of elements in an array by using count() and sizeof() functions. Using count() Function: The count() function is used to get the total number of elements in an array. Syntax: count(arra
2 min read
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
PHP array_merge_recursive() Function The array_merge_recursive() is an inbuilt function in PHP and is used to merge two or more arrays into a single array recursively. 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 arra
3 min read