How7o
  • Home
  • Tools
  • Prank Screens
  • Learn
  • Blog
  • Contact
Reading: How to Combine Two Arrays Without Duplicates in PHP
Share
How7oHow7o
Font ResizerAa
  • OS
Search
  • Home
  • Tools
  • Prank Screens
  • Learn
  • Blog
  • Contact
Follow US
© 2024–2026 How7o. All rights reserved.
How7o > Free Laravel, PHP, WordPress & Server Tutorials > 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.
[mc4wp_form]
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
Most Popular
Set vi as the default editor in Ubuntu — a terminal opening the vim editor
How to Set vi (Vim) as the Default Editor in Ubuntu
June 8, 2026
rsync says ALL DONE but files are missing — a terminal showing ALL DONE next to an empty folder
rsync Says “ALL DONE” but Files Are Missing: How to Verify
June 8, 2026
Migrate a website to a new server with rsync — files copying from an old server to a new one over SSH
How to Migrate a Website to a New Server With rsync
June 8, 2026
Bun runtime — faster JS toolkit replacing npm in Laravel projects
How to Install Bun Runtime on Ubuntu (And Use It in a Laravel Project)
May 24, 2026
Tailscale mesh — peer-to-peer connections between devices, coordination server
How to Install Tailscale on Ubuntu (Zero-Config Mesh VPN for Self-Hosters)
May 24, 2026

You Might Also Like

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

Fix “403 Forbidden” on Laravel Shared Hosting

8 Min Read
Securely hash passwords in PHP with password_hash
Web Development

Securely Hash Passwords in PHP (password_hash, Argon2id)

6 Min Read
Laravel Eloquent exists method checking if a record exists in a database query
Web Development

How to Check if a Record Exists in Laravel

6 Min Read
WordPress check if user is logged in with is_user_logged_in()
Web Development

How to Check If a User Is Logged In in WordPress

7 Min Read
How7o

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

Tools

  • Age Calculator
  • Word Counter
  • Image Upscaler
  • Password Generator
  • QR Code Generator
  • See all tools→

Pranks

  • Fake Blue Screen Prank
  • Hacker Typer
  • Fake iMessage Generator
  • Windows XP Crash Prank
  • Windows 11 Update Prank
  • See all prank screens →

Company

  • About Us
  • Blog
  • Contact
  • Privacy Policy
  • Terms of Service
  • Sitemap
© 2024–2026 How7o. All rights reserved.
Welcome Back!

Sign in to your account

Username or Email Address
Password

Lost your password?