PHP array_intersect_assoc() function

P
PHP array_intersect() function
PHP array_intersect_key() function

The array_intersect_assoc function is an advanced version of the array_intersect function which we studied in the previous tutorial. It compares both the keys and values of two or more arrays and returns the matching result.

It compares the keys and values from the first array with the keys and values of other arrays and returns an associative array that contains the common/matching keys and values.

What is the syntax of the array_intersect_assoc function in PHP?

array_intersect_assoc(array1,array2,array3, ...)
ParametersDetails
array1Array to make comparison from – Required
array2Array to compare with – Required
array3,…Further arrays to compare with – Required
array_intersect_assoc function in PHP

Examples of array_intersect_assoc function

<?php
$a1=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow");
$a2=array("a"=>"red","b"=>"green","c"=>"blue");

$result=array_intersect_assoc($a1,$a2);
print_r($result);
?>

In the above example, the array_intersect function compares the keys and values of two arrays and returns the matches.

<?php
$a1=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow");
$a2=array("a"=>"red","b"=>"green","g"=>"blue");
$a3=array("a"=>"red","b"=>"green","g"=>"blue");

$result=array_intersect_assoc($a1,$a2,$a3);
print_r($result);
?>

In the above example, the array_intersect function compares the keys and values of three arrays and returns the matches.

PHP array_intersect() function
PHP array_intersect_key() function

Add comment

Tutor Network

Share this

Learn PHP from A to Z