PHP array_intersect_assoc()Functions

PHP Array ReferenceComplete PHP Array Reference

Examples

Compare thekeys and valuesof two arrays, and return the intersection:

<?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);
?>

Run example »

Definition and Usage

The array_intersect_assoc() function compares thekeys and valuesof two (or more) arrays, and returns the intersection.

This function compares the keys and values of two (or more) arrays and returns an intersection array that includes all the keys and values in the compared array (array1) that are also present in any other parameter array (array2orarray3etc.).


Syntax

array_intersect_assoc(array1,array2,array3...)

Parameter Description
array1 Required. The first array to compare with other arrays.
array2 Required. The array to compare with the first array.
array3,... Optional. Other arrays to compare with the first array.

Technical Details

Return value: Returns an intersection array that includes all the keys and values in the compared array (array1) that are also present in any other parameter array (array2orarray3etc.).
PHP Version: 4.3.0+


More Examples

Example 1

Compare thekeys and valuesof three arrays, and return the intersection:

<?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);
?>

Run example »


PHP Array ReferenceComplete PHP Array Reference Other Extensions