Manipulating array data structures is a fundamental operation in PHP backend applications. Whether you are aggregating user inputs, merging database query results, or combining configuration settings, combining multiple arrays while filtering out duplicate values requires understanding PHP's native array utilities.

In PHP, combining arrays and eliminating duplicates can be achieved using array_merge(), array_unique(), the modern array unpack spread operator ([...$a, ...$b]), and the array union operator (+). In this article, we examine these methods alongside key re-indexing techniques.

Quick Syntax Comparison Table

Compare behavior across PHP array merging strategies:

quick_comparison.phpphp
<?php
 
$a = ['apple', 'banana', 'cherry'];
$b = ['banana', 'dragonfruit', 'apple'];
 
// 1. array_merge() + array_unique() + array_values()
$mergedUnique = array_values(array_unique(array_merge($a, $b)));
// Result: ['apple', 'banana', 'cherry', 'dragonfruit']
 
// 2. Spread Operator [...$a, ...$b] (PHP 7.4+)
$spreadUnique = array_values(array_unique([...$a, ...$b]));
// Result: ['apple', 'banana', 'cherry', 'dragonfruit']
 
// 3. Array Union Operator ($a + $b)
$union = $a + $b; 
// Caution: Retains left-hand keys! $union[0] stays 'apple', $union[1] stays 'banana'

1. Combining array_merge() and array_unique()

The standard approach to combining two or more indexed arrays and extracting unique elements involves wrapping array_merge() inside array_unique():

array_merge_example.phpphp
<?php
 
$list1 = ['PHP', 'Python', 'JavaScript'];
$list2 = ['Go', 'Python', 'Rust', 'PHP'];
 
// Step 1: Merge arrays (duplicates present)
$merged = array_merge($list1, $list2);
// Output: ['PHP', 'Python', 'JavaScript', 'Go', 'Python', 'Rust', 'PHP']
 
// Step 2: Extract unique elements (preserves original key indices)
$uniqueWithGaps = array_unique($merged);
// Output keys: [0 => 'PHP', 1 => 'Python', 2 => 'JavaScript', 3 => 'Go', 5 => 'Rust']
 
// Step 3: Reset numeric array keys sequentially using array_values()
$cleanUniqueList = array_values($uniqueWithGaps);
// Output keys: [0 => 'PHP', 1 => 'Python', 2 => 'JavaScript', 3 => 'Go', 4 => 'Rust']
 
print_r($cleanUniqueList);

2. Modern PHP 7.4+ Array Unpacking ([...$a, ...$b])

Starting with PHP 7.4, array unpacking via the spread operator ... provides a faster, cleaner syntax for merging indexed arrays:

spread_operator_example.phpphp
<?php
 
$frontend = ['HTML', 'CSS', 'JavaScript'];
$backend = ['Node.js', 'PHP', 'JavaScript'];
 
// Unpack arrays cleanly and pass directly to array_unique
$techStack = array_values(array_unique([...$frontend, ...$backend]));
 
print_r($techStack);
// Result: ['HTML', 'CSS', 'JavaScript', 'Node.js', 'PHP']

3. Associative Arrays: array_merge() vs Array Union Operator (+)

For associative arrays with string keys, array_merge() overwrites duplicate string keys with values from the right-hand array, whereas the + union operator preserves values from the left-hand array:

associative_merge.phpphp
<?php
 
$defaultConfig = ['theme' => 'light', 'debug' => false, 'version' => 1];
$userConfig    = ['theme' => 'dark',  'debug' => true];
 
// Behavior A: array_merge() - Overwrites matching string keys
$mergedConfig = array_merge($defaultConfig, $userConfig);
// Result: ['theme' => 'dark', 'debug' => true, 'version' => 1]
 
// Behavior B: + Operator - Retains left-hand values, ignores right-hand duplicates
$unionConfig = $defaultConfig + $userConfig;
// Result: ['theme' => 'light', 'debug' => false, 'version' => 1]

Memory Overhead in PHP Zend Engine (zend_array)

  • Zend HashTable Allocation: PHP arrays are implemented internally as dynamic HashTables (zend_array). Chaining array_unique(array_merge($a, $b)) allocates intermediate memory structures for the merged array before deduplication.

  • String vs Numeric Flags: By default, array_unique() evaluates elements using SORT_STRING. For integer arrays, pass SORT_NUMERIC (array_unique($merged, SORT_NUMERIC)) to avoid CPU-intensive string typecasting during comparisons.