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

PHP filter_input: How to Validate Input Securely

User input can be risky. Hackers exploit weak validation to inject malicious data. PHP filter_input() helps you sanitize and validate…

PHP array_all Function: How it Works with Examples

PHP 8.4 released a new built-in function, array_all checks to check if all array values meet a condition. It gives…

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 Logical Operators | Understanding the 6 Logical Operators

The PHP logical operators are operands that can be used to check two or more expressions together. For examples (…

PHP Assignment Operators: A Complete Guide

The assignment operators are the sets of gears that keep your code well-oiled and running when deep in PHP, allowing…

PHP array_merge_recursive: Merge Arrays Deeply

PHP array_merge_recursive joins two or more arrays into one nested array and keeps all values with their keys. Syntax of…

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 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 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…

Mastering PHP’s Do-While Loop: Examples and Explanation

The PHP do-while loop is a type of loop that executes a block of code at least once, and then…

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.