Introduction
When developers need to combine or merge arrays. PHP provides several built-in functions to perform these operations. Two important functions used for combining arrays are:
- array_merge() is used to merge two or more arrays together.
- array_combine() is used to create a new array using one array as keys and another array as values.
Understanding the difference between array_merge() vs array_combine() helps developers manage arrays more effectively.
What is array_merge() in PHP?
The array_merge() function is used to merge two or more arrays into a single array.
When arrays are merged:
- Values from all arrays are added to one array.
- Numeric keys are reindexed automatically.
- If there are duplicate string keys, the later value overwrites the previous value.
Syntax of array_merge()
Parameters
- array1 – The first array
- array2 – The second array
- Additional arrays can also be added
Example of array_merge()
$array1 = array("PHP", "Java");
$array2 = array("Python", "JavaScript");
$result = array_merge($array1, $array2);
print_r($result);
Output
[1] => Java
[2] => Python
[3] => JavaScript
)
Explanation
In this example:
- Two arrays are created.
- The array_merge() function combines them into one array.
- The elements are placed sequentially.
array_merge() with Associative Arrays
When associative arrays are merged, duplicate keys are replaced by the latest value.
$array1 = array("name" => "Rahul", "age" => 20);
$array2 = array("age" => 25, "city" => "Delhi");
$result = array_merge($array1, $array2);
print_r($result);
Output
[age] => 25
[city] => Delhi
)
Explanation
Here:
- Both arrays contain the key age.
- The value 25 replaces 20.
What is array_combine() in PHP?
The array_combine() function is used to create a new array by combining two arrays.
- The first array becomes the keys
- The second array becomes the values
Both arrays must contain the same number of elements, otherwise PHP will generate an error.
Syntax of array_combine()
array_combine(keys, values);
Parameters
- keys – Array containing keys
- values – Array containing values
Example of array_combine()
$keys = array("name", "age", "city");
$values = array("Ravi", 22, "Mumbai");
$result = array_combine($keys, $values);
print_r($result);
Output
[age] => 22
[city] => Mumbai
)
Explanation
Here:
- The first array provides keys.
- The second array provides values.
- A new associative array is created.
Important Rule of array_combine()
Both arrays must have an equal number of elements.
Example of incorrect usage:
$keys = array("name", "age");
$values = array("Ravi");
array_combine($keys, $values);
Output
Warning: array_combine(): Both parameters should have equal number of elements
Difference Between array_merge() and array_combine()
Although both functions deal with arrays, their purpose is different.
| Feature | array_merge() | array_combine() |
|---|---|---|
| Purpose | Merges arrays | Creates new associative array |
| Input | Multiple arrays | Two arrays |
| Key Handling | Numeric keys reindexed | First array becomes keys |
| Value Handling | Values merged sequentially | Second array becomes values |
| Element Requirement | Any number of elements | Equal elements required |
Example Comparing Both Functions
$array1 = array("A", "B", "C");
$array2 = array("X", "Y", "Z");
$result1 = array_merge($array1, $array2);
$result2 = array_combine($array1, $array2);
print_r($result1);
print_r($result2);
Output
array_merge()
(
[0] => A
[1] => B
[2] => C
[3] => X
[4] => Y
[5] => Z
)
array_combine()
(
[A] => X
[B] => Y
[C] => Z
)
Explanation
- array_merge() joins arrays sequentially.
- array_combine() maps one array to another.
Real Life Example
Suppose you have two arrays from a database:
$columns = array("id", "name", "email");
$data = array(101, "Rohit", "rohit@gmail.com");
//Using array_combine():
$user = array_combine($columns, $data);
print_r($user);
Output:
(
[id] => 101
[name] => Rohit
[email] => rohit@gmail.com
)
This converts raw data into a structured associative array.
When Should You Use array_merge()?
Use array_merge() when you want to:
- Join multiple arrays
- Combine list data
- Merge results from different sources
Example:
array_merge($array1, $array2);
When Should You Use array_combine()?
Use array_combine() when you want to:
- Convert two arrays into a key-value pair
- Create associative arrays
- Map database columns to values
Example:
array_combine($keys, $values);
Conclusion
The array_merge() function is used to combine multiple arrays into a single array, making it useful for merging list data or combining results from different sources.
On the other hand, array_combine() creates a new associative array by using one array as keys and another as values. This is especially useful when converting raw data into structured key-value pairs.
array_merge() vs array_combine() – Objective Questions (MCQs)
Q1. Which function is used to merge two or more arrays in PHP?
Q2. What does array_combine() require?
Q3. What happens if the arrays passed to array_combine() have different sizes?
Q4. How does array_merge() handle duplicate string keys?
Q5. Which function creates an array using one array for keys and another for values?