How7o
  • Home
  • Tools
  • Prank Screens
  • Contact
  • Blog
Reading: How to Combine Two Arrays Without Duplicates in PHP
Share
Subscribe Now
How7oHow7o
Font ResizerAa
  • Marketing
  • OS
  • Features
  • Guide
  • Complaint
  • Advertise
Search
  • Home
  • Tools
  • Prank Screens
  • Contact
  • Blog
Follow US
Copyright © 2014-2023 Ruby Theme Ltd. All Rights Reserved.
How7o > Blog > Web Development > How to Combine Two Arrays Without Duplicates in PHP
Web Development

How to Combine Two Arrays Without Duplicates in PHP

how7o
By how7o
Last updated: May 10, 2026
7 Min Read
PHP merge arrays without duplicates — union operator and array_unique
SHARE

When you php merge arrays unique and want to preserve the original keys, array_merge is the wrong tool — it re-numbers integer keys and drops them. Use the + (union) operator for associative merges, array_unique(array_merge(...)) for dedup-by-value on numeric arrays, or array_merge_recursive for deep-nested config-style merges. This guide shows which pattern matches which shape of input.

Contents
  • TL;DR
  • Keep original keys — the + operator
  • Numeric arrays — array_unique(array_merge(...))
  • Deep nested — array_merge_recursive
  • Decision table
  • Frequently asked questions
  • Related guides
  • References

Last verified: 2026-04-23 on PHP 8.3. Originally published 2023-02-07, rewritten and updated 2026-04-23.

TL;DR

// Keep keys, dedup by key — use the + operator
$output = $array1 + $array2;

// Numeric arrays, dedup by value — array_unique + array_merge
$output = array_unique(array_merge($array1, $array2));

// Deep merge (nested associative)
$output = array_merge_recursive($array1, $array2);

Keep original keys — the + operator

$array1 = [
    '56' => '56',
    '57' => '57',
    '58' => '58',
    '59' => '59',
    '60' => '60',
];

$array2 = [
    '58' => '58',
    '59' => '59',
    '60' => '60',
    '61' => '61',
    '62' => '62',
];

$output = $array1 + $array2;
// [56 => '56', 57 => '57', 58 => '58', 59 => '59', 60 => '60', 61 => '61', 62 => '62']

The + operator preserves every key from both arrays. On a key collision, the value from the left-hand array wins and the right-hand value is dropped. This is the inverse of array_merge‘s string-key behavior — which is why it’s the right tool when you care about specific keys (record IDs, user IDs, taxonomy term IDs).

php merge arrays unique — plus operator keeps keys, array_unique+array_merge dedups by value

Numeric arrays — array_unique(array_merge(...))

$array1 = ['56', '57', '58', '59', '60'];
$array2 = ['58', '59', '60', '61', '62'];

$output = array_unique(array_merge($array1, $array2));
// ['56', '57', '58', '59', '60', 4=>'61', 5=>'62']  — keys from first occurrence

When the arrays are positional (no meaningful keys), first concatenate, then dedup by value. array_unique keeps the first occurrence of each value and drops later duplicates. The resulting keys reflect where each value first appeared, which may not be 0-indexed contiguously. If you need a clean [0, 1, 2, ...] keyspace, add array_values():

$output = array_values(array_unique(array_merge($array1, $array2)));
// ['56', '57', '58', '59', '60', '61', '62']

Deep nested — array_merge_recursive

$defaults = [
    'db' => [
        'host' => 'localhost',
        'port' => 3306,
    ],
    'cache' => 'file',
];

$overrides = [
    'db' => [
        'host' => 'db.prod',
    ],
    'cache' => 'redis',
];

$config = array_merge_recursive($defaults, $overrides);
// [
//   'db' => [ 'host' => ['localhost', 'db.prod'], 'port' => 3306 ],
//   'cache' => ['file', 'redis'],
// ]

Watch out: array_merge_recursive combines scalar values into sub-arrays rather than replacing them. Usually not what you want for config merges. For “override scalar, merge nested arrays” semantics, reach for a helper like array_replace_recursive:

$config = array_replace_recursive($defaults, $overrides);
// [
//   'db' => [ 'host' => 'db.prod', 'port' => 3306 ],
//   'cache' => 'redis',
// ]

array_replace_recursive overwrites scalar values while still diving into nested arrays — the behavior most config-merge code actually wants.

Decision table

GoalFunction
Keep original keys, first array wins collisions$a + $b
Keep original keys, second array wins collisionsarray_replace($a, $b)
Reindex integer keys, remove value duplicatesarray_values(array_unique(array_merge($a, $b)))
Deep merge (config override)array_replace_recursive($a, $b)
Deep merge (combine scalars into arrays)array_merge_recursive($a, $b)

Frequently asked questions

Why does array_merge lose my keys when I try to php merge arrays unique?

array_merge re-numbers integer keys starting from 0 — that’s documented behavior. String keys are preserved (the second array’s value wins on collision). For arrays with integer keys you want to keep, use the + (union) operator instead: $merged = $array1 + $array2; — it keeps both arrays’ original keys and, on collision, keeps the value from the first array.

When does the + operator lose data?

On key collisions — if both arrays have key 5, the second array’s value is dropped. That’s fine when keys uniquely identify rows (e.g., record IDs). It’s wrong when both arrays have overlapping keys that should merge content. For that case, iterate: foreach ($array2 as $k => $v) { $array1[$k] ??= $v; } keeps array1’s value when both exist.

How do I remove duplicate values regardless of key?

array_unique($array) — keeps the first occurrence of each value and drops later duplicates. Combine with array_merge for the merge-unique pattern: $out = array_unique(array_merge($array1, $array2)). The keys in the result come from the first-occurrence positions, which may not be contiguous — follow with array_values() if you need a clean 0-indexed list.

Is there a difference between array_unique with objects?

Yes — array_unique compares using SORT_STRING by default, which calls (string) on each element. For objects, that usually produces "Object id #N" which isn’t a real uniqueness check. Pass SORT_REGULAR as the second argument for value comparison, or for complex objects write a manual loop with your own equality check.

What about merging deeply nested arrays?

array_merge_recursive($array1, $array2) — for shared keys it combines values into sub-arrays instead of overwriting. Useful for config-like structures where one array extends another. For WordPress multisite or Laravel config overrides, this is the standard way to stack deep preferences.

Related guides

  • How to Delete an Element from a PHP Array — the sibling array-removal pattern.
  • How to Convert a String to Uppercase in PHP — another PHP basics post.
  • How to Convert a String to Float in PHP — cleaning values before adding them to an array.
  • How to Retrieve Inputs with a Specific Prefix in Laravel — real-world array filtering with collect.

References

PHP array merge and union: php.net/manual/en/function.array-merge. Union operator: php.net/manual/en/language.operators.array.

TAGGED:php

Sign Up For Daily Newsletter

Be keep up! Get the latest breaking news delivered straight to your inbox.
By signing up, you agree to our Terms of Use and acknowledge the data practices in our Privacy Policy. You may unsubscribe at any time.
Share This Article
Facebook Copy Link Print
Previous Article PHP delete array element — unset, array_splice, array_filter, array_search How to Delete an Element from a PHP Array
Next Article PHP string to float conversion with cast, regex cleanup, NumberFormatter How to Convert a String to Float in PHP
Leave a Comment

Leave a Reply Cancel reply

You must be logged in to post a comment.

FacebookLike
XFollow
PinterestPin
InstagramFollow

Subscribe Now

Subscribe to our newsletter to get our newest articles instantly!
Most Popular
Display PHP errors — ini_set + php.ini configuration
How to Display PHP Errors
May 10, 2026
PHP convert string to uppercase — strtoupper and mb_strtoupper
How to Convert a String to Uppercase in PHP
May 10, 2026
PHP string to float conversion with cast, regex cleanup, NumberFormatter
How to Convert a String to Float in PHP
May 10, 2026
PHP merge arrays without duplicates — union operator and array_unique
How to Combine Two Arrays Without Duplicates in PHP
May 10, 2026
PHP delete array element — unset, array_splice, array_filter, array_search
How to Delete an Element from a PHP Array
May 10, 2026

You Might Also Like

Laravel last inserted ID — Eloquent save populates model primary key illustration
Web Development

How to Retrieve the Last Inserted ID in Laravel Eloquent

8 Min Read
Laravel 403 forbidden on shared hosting — root htaccess rewrite into public folder
Web Development

Fix “403 Forbidden” on Laravel Shared Hosting

8 Min Read
WooCommerce product view counter — meta-based counter with increment and display hooks
Web Development

How to Display a Product View Counter in WooCommerce Without a Plugin

7 Min Read
Laravel DataTables HTML column — rawColumns opt-out of the default escaping
Web Development

How to Add an HTML Column in Laravel DataTables

7 Min Read
How7o

We provide tips, tricks, and advice for improving websites and doing better search.

Latest News

  • SEO Audit Tool
  • Client ReferralsNew
  • Execution of SEO
  • Reporting Tool

Resouce

  • Google Search Console
  • Google Keyword Planner
  • Google OptimiseHot
  • SEO Spider

Get the Top 10 in Search!

Looking for a trustworthy service to optimize the company website?
Request a Quote
Welcome Back!

Sign in to your account

Username or Email Address
Password

Lost your password?