PHP array_intersect_assoc: How it Works with Examples

php array_intersect_assoc

PHP array_intersect_assoc compares values and keys in arrays. It returns matches with the same key and value from all arrays you pass.

Understand the array_intersect_assoc in PHP

The array_intersect_assoc function compares both keys and values. It checks each pair across arrays and returns elements that match in both value and key.

The syntax looks like this:

array_intersect_assoc($array1, $array2, ...$arrays)
  • $array1: the base array for comparison.
  • $array2: the second array to check against.
  • ...$arrays: more arrays to check.
  • It returns an array with elements that match in key and value.

Here is a quick example:

$a = ["x" => 10, "y" => 20, "z" => 30];
$b = ["x" => 10, "y" => 25, "z" => 30];
$result = array_intersect_assoc($a, $b);
print_r($result);

The output will be:

Array
(
[x] => 10
[z] => 30
)

This result shows both the key and the value match in those pairs.

The function checks $array1 and compares each element with other arrays. It checks both the value and the key. If both match, then it keeps the element in the result.

The Difference Between array_intersect and array_intersect_assoc

The array_intersect compares only the values. It does not check the keys.

The array_intersect_assoc compares both values and keys. It returns only elements that match in both.

Here is a table showing you the key differences:

FunctionCheck ValuesCheck Keys
array_intersectYesNo
array_intersect_assocYesYes

Use array_intersect when you only need value matches. Use array_intersect_assoc when key match matters in your logic.

Examples of array_intersect_assoc in PHP

Match Keys and Values:

$a = ["id" => 1, "age" => 22, "score" => 85];
$b = ["id" => 1, "age" => 25, "score" => 85];
$result = array_intersect_assoc($a, $b);
print_r($result);

This returns keys id and score because both key and value match in both arrays.

Here is the output:

Array
(
[id] => 1
[score] => 85
)

Compare Three Arrays:

$a = ["a" => 100, "b" => 200, "c" => 300];
$b = ["a" => 100, "b" => 250, "c" => 300];
$c = ["a" => 100, "c" => 300, "d" => 400];
$result = array_intersect_assoc($a, $b, $c);
print_r($result);

The result contains a and c. Both have the same key and the same value across all three arrays.

The output:

Array
(
[a] => 100
[c] => 300
)

Compare with Mixed Keys:

$a = [10 => "x", 20 => "y", 30 => "z"];
$b = [10 => "x", 20 => "y", 25 => "z"];
$result = array_intersect_assoc($a, $b);
print_r($result);

The result has keys 10 and 20 because they match in both key and value. The output:

Array
(
[10] => x
[20] => y
)

Nested Array Values:

$a = ["data" => [1, 2], "code" => 300];
$b = ["data" => [1, 2], "code" => 400];
$result = array_intersect_assoc($a, $b);
print_r($result);

The function compares the arrays inside data and finds that it is equal in both arrays. This shows you two warning messages.

Here is the output:

Array
(
[data] => Array
(
[0] => 1
[1] => 2
)

)

PHP Warning: Array to string conversion in index.php on line 6
PHP Warning: Array to string conversion in index.php on line 6

That is because the array_intersect_assoc uses comparison operators. So, when it tries to compare values, it may attempt string conversion if one side is not an array.

Here is the correct code:

$a = ["data" => [1, 2], "code" => 300];
$b = ["data" => [1, 2], "code" => 400];

$result = [];
foreach ($a as $key => $value) {
    if (array_key_exists($key, $b) && $value === $b[$key]) {
        $result[$key] = $value;
    }
}
print_r($result);

The output:

Array
(
[data] => Array
(
[0] => 1
[1] => 2
)

)

Wrapping Up

You learned what array_intersect_assoc does and how it works with syntax and return values. You also saw how it differs from array_intersect and why that matters.

Here is a quick recap:

  • array_intersect_assoc checks keys and values.
  • array_intersect checks values only.
  • You can pass more than two arrays.
  • The result only keeps the key-value pairs that are the same in every array.

FAQs

What does PHP array_intersect_assoc do?

It compares arrays and returns values with matching keys.
$a = ["id" => 1, "name" => "Ali", "role" => "admin"];
$b = ["id" => 1, "name" => "Ali", "role" => "user"];
$result = array_intersect_assoc($a, $b);
print_r($result);
  • It checks both keys and values.
  • Only identical key-value pairs remain.

What is the difference between array_intersect and array_intersect_assoc?

The first checks values only, the second checks keys and values together.
$a = ["x" => 10, "y" => 20];
$b = ["y" => 20, "z" => 10];

print_r(array_intersect($a, $b));
print_r(array_intersect_assoc($a, $b));
  1. array_intersect returns matches by value.
  2. array_intersect_assoc returns matches by key and value.

Can array_intersect_assoc handle numeric keys?

Yes, but the numeric keys must also match exactly along with the values.
$a = [0 => "red", 1 => "blue", 2 => "green"];
$b = [1 => "blue", 2 => "yellow", 0 => "red"];

print_r(array_intersect_assoc($a, $b));
  • Key 0 with "red" matches.
  • Key 1 with "blue" matches.
  • Key 2 fails since values differ.

Similar Reads

How to Select Data Using PHP and MySQL?

In this article, You will understand everything you need to know about how to use PHP to select data from…

PHP array_map Function: How to Transform Arrays with Examples

PHP added the array_map() function to help you apply one function to each item in an array. Understand the array_map…

How to Connect a MySQL Database to PHP?

Connecting PHP to MySQL is not just a technical step—it is the basic part of how data-driven websites work. From…

PHP substr Function: How to Extract and Manipulate Strings

The substr() function in PHP returns part of a string. You give it a string, a starting point, and optionally…

PHP trim(): How to remove Whitespace from String

Early PHP scripts often failed because of extra spaces or line breaks in strings. These issues came from user input…

PHP Namespace: How to Group Code (With Example)

PHP namespace solves the problem of name conflicts. Different developers may create functions-classes, or constants with the same name. PHP…

PHP Spread Operator: Understand How (…) Syntax Works

Before the spread operator, PHP developers had to pass array items to a function using extra steps. They often had…

PHP Comment: How to Write Comments in PHP

let's now dive into PHP comments—these simple but super important integral parts of PHP coding. You can think of comments…

PHP mb_strtolower: How to Handle Multibyte Strings Properly

The mb_strtolower() function in PHP turns every letter in a string into lowercase. It works with multibyte encodings like UTF-8.…

PHP mail() Function: A Complete Guide

PHP mail() is a great built-in function for sending an email directly from your script for account confirmations, password resets, or sending notifications.…

Previous Article

HTML Link Attributes Guide for Beginners

Next Article

JavaScript Logical Operators with Examples

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.