0
0
PHPprogramming~5 mins

Array merge and combine in PHP

Choose your learning style9 modes available
Introduction

We use array merge and combine to join two or more lists into one. This helps us organize and work with data easily.

You have two lists of names and want to put them together into one list.
You want to create a new list where one list's values become keys and the other's values become values.
You want to add items from one list to the end of another list.
You want to pair two lists to create a dictionary-like structure.
Syntax
PHP
<?php
// Merge arrays
$mergedArray = array_merge($array1, $array2);

// Combine arrays (keys and values)
$combinedArray = array_combine($keysArray, $valuesArray);
?>

array_merge joins arrays by adding elements from the second array to the first.

array_combine creates a new array using one array for keys and another for values. Both arrays must have the same number of elements.

Examples
Merges two arrays into one list with all fruits.
PHP
<?php
$array1 = ["apple", "banana"];
$array2 = ["cherry", "date"];
$merged = array_merge($array1, $array2);
print_r($merged);
?>
Combines keys and values into an associative array.
PHP
<?php
$keys = ["a", "b", "c"];
$values = [1, 2, 3];
$combined = array_combine($keys, $values);
print_r($combined);
?>
Merging two empty arrays results in an empty array.
PHP
<?php
// Edge case: empty arrays
$empty1 = [];
$empty2 = [];
$mergedEmpty = array_merge($empty1, $empty2);
print_r($mergedEmpty);
?>
Combining arrays with one element each creates a single key-value pair.
PHP
<?php
// Edge case: one element arrays
$oneKey = ["x"];
$oneValue = [42];
$combinedOne = array_combine($oneKey, $oneValue);
print_r($combinedOne);
?>
Sample Program

This program shows how to merge two fruit lists and how to combine keys and values into an associative array.

PHP
<?php
// Create two arrays
$fruits1 = ["apple", "banana"];
$fruits2 = ["cherry", "date"];

// Show original arrays
echo "Original arrays:\n";
print_r($fruits1);
print_r($fruits2);

// Merge arrays
$allFruits = array_merge($fruits1, $fruits2);
echo "\nAfter merging:\n";
print_r($allFruits);

// Create keys and values arrays
$keys = ["first", "second", "third"];
$values = ["red", "green", "blue"];

// Show keys and values
echo "\nKeys and values arrays:\n";
print_r($keys);
print_r($values);

// Combine keys and values
$colorMap = array_combine($keys, $values);
echo "\nAfter combining keys and values:\n";
print_r($colorMap);
?>
OutputSuccess
Important Notes

Time complexity: array_merge runs in O(n + m) where n and m are the sizes of the arrays.

Space complexity: Both functions create new arrays, so space grows with input size.

Common mistake: Using array_combine with arrays of different lengths causes an error.

Use array_merge when you want to join lists. Use array_combine when you want to create key-value pairs.

Summary

array_merge joins two or more arrays into one list.

array_combine creates an associative array from two lists: one for keys, one for values.

Both are useful to organize and work with data in PHP.