PHP array_diff_assoc: How to Compare Arrays with Keys

php array diff assoc function

Array values may look the same, but keys can differ. The array_diff_assoc in PHP finds differences by both values and keys at once.

Understand the array_diff_assoc Function in PHP

The array_diff_assoc() function compares two or more arrays by both value and key. It returns the parts from the first array that do not exist in the others with the same key.

Here is the syntax:

array_diff_assoc($array1, $array2, ...$arrays)
  • $array1: the main array for comparison.
  • $array2, ...$arrays: one or more arrays to check against.
  • Returns: array of values from $array1 that do not match in both key and value.

The function checks each element of the first array. It looks at both the key and the value. If another array has the same key but a different value, the element stays in the result. If the key or value match, the element drops out.

Here is a quick example:

$first = ["a" => 1, "b" => 2, "c" => 3];
$second = ["a" => 1, "b" => 5, "d" => 3];
$result = array_diff_assoc($first, $second);

print_r($result);

The output will be:

Array
(
[b] => 2
[c] => 3
)

It returns key-value pairs from the first array that don’t match in the second.

The Difference Between array_diff and array_diff_assoc

The array_diff() looks only at values, while the array_diff_assoc() looks at both keys and values. Use the first one when keys don’t matter, use the second when they do.

  • array_diff() → values only.
  • array_diff_assoc() → keys and values.

You can use the array_diff_assoc() when the strict comparison of arrays that must include both the key and the value.

Examples of the array_diff_assoc in PHP

Compare Two Arrays with Different Keys:

$first = ["x" => 10, "y" => 20, "z" => 30];
$second = ["x" => 10, "a" => 20, "z" => 25];
$result = array_diff_assoc($first, $second);

print_r($result);

This example returns y => 20 and z => 30 because the first array has no match for key y and a different value for z.

The output:

Array
(
[y] => 20
[z] => 30
)

Compare Three Arrays:

$first = ["id" => 101, "name" => "Ali", "role" => "Admin"];
$second = ["id" => 101, "name" => "Ali", "role" => "User"];
$third = ["id" => 101, "name" => "Omar", "role" => "Admin"];
$result = array_diff_assoc($first, $second, $third);

print_r($result);

It returns an empty array because every key-value in the first array appears in at least one of the others.

Compare Arrays with Nested Keys:

$first = ["p1" => "Book", "p2" => "Pen", "p3" => "Pad"];
$second = ["p1" => "Book", "p2" => "Pencil", "p3" => "Pad"];
$result = array_diff_assoc($first, $second);

print_r($result);

The output:

Array
(
[p2] => Pen
)

This example returns p2 => Pen because both arrays share the same key, but the value is different.

Strict Key and Value Match:

$first = [0 => "Car", 1 => "Bus", 2 => "Bike"];
$second = [0 => "Car", 2 => "Bike", 3 => "Train"];
$result = array_diff_assoc($first, $second);

print_r($result);

This example returns 1 => Bus because the first array has a key and value pair that does not match in the second array.

Wrapping Up

You learned the syntax of array_diff_assoc and how it checks both keys and values. You also learned how it differs from array_diff and how to use it in real code.

Here is a quick recap:

  • array_diff_assoc() helps you to compare arrays by both keys and values.
  • It returns unmatched parts from the first array.
  • You can use it when a strict comparison of key and value is required.
  • It differs from array_diff() because that one ignores keys.

FAQs

What does PHP array_diff_assoc do?

PHP array_diff_assoc compares arrays and checks both keys and values. It returns elements from the first array that are not present in others.
  • Syntax: array_diff_assoc($array1, $array2, ...);
  • Checks: Both key and value are compared strictly.
$a = ["id" => 101, "name" => "Ali"];
$b = ["id" => 101, "name" => "Omar"];
print_r(array_diff_assoc($a, $b));

What is the difference between array_diff and array_diff_assoc?

array_diff compares only values. array_diff_assoc compares both values and their keys.
  • array_diff: Ignores keys.
  • array_diff_assoc: Considers both keys and values.
$a = ["id" => 101, "name" => "Ali"];
$b = ["id" => 101, "name" => "Ali"];
print_r(array_diff($a, $b));        // []
print_r(array_diff_assoc($a, $b));  // []

How do you use array_diff_assoc with multiple arrays?

You can compare more than two arrays. array_diff_assoc removes keys and values that match in any given array.
  1. Pass the first array.
  2. Add one or more arrays to compare.
  3. Result keeps unique key-value pairs.
$a = ["id" => 101, "name" => "Ali", "city" => "Riyadh"];
$b = ["id" => 101, "name" => "Omar"];
$c = ["city" => "Riyadh"];
print_r(array_diff_assoc($a, $b, $c));

Similar Reads

PHP fwrite: How to Write Data to Files in PHP

actions, store settings, or even create log files for debugging. That’s where PHP fwrite comes in. It writes data to…

PHP Hello World: Write Your First Program with Code Example

You always start with “Hello World” when you learn a new language. It keeps things simple. You see right away…

PHP if-elseif Statement: ‘elseif’ versus ‘else if’

PHP’s if-elseif statement is a fundamental construct that allows developers to control the flow of their code based on different conditions. In…

PHP array_intersect_ukey Function: How it Works with Examples

The array_intersect_ukey is used to compare arrays by keys with a custom function in PHP. It returns matches by keys…

PHP Spaceship Operator

If you are new to the PHP Spaceship Operator, then let me tell you, you're in for a treat. This…

PHP require_once and require

Essentially, “require” and “require_once” are directives in PHP to include and evaluate a specific file during the execution of a…

PHP echo vs print: Key Differences & Usage Guide

Echo and print are foundational in displaying output in PHP, and though they might be similar in some ways, they…

PHP Type Juggling: How it Works Behind the Scenes

PHP type juggling refers to the dynamic system where the type of a variable is determined by its context in…

PHP SimpleXML: Work with XML in Examples

PHP SimpleXML - invented for you to work with XML. Way leaner, quicker to get up. it's already integrated directly…

PHP array_keys: How to Extract Keys in Arrays with Examples

This guide shows how PHP array_keys finds all keys in an array and how you can filter them by a…

Previous Article

Bitwise Operators in JavaScript

Next Article

HTML Document Structure Guide for Beginners

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *


Subscribe to Get Updates

Get the latest updates on Coding, Database, and Algorithms straight to your inbox.
No spam. Unsubscribe anytime.