Home >>PHP Array Functions >PHP array_intersect_assoc() Function

PHP array_intersect_assoc() Function

PHP array_intersect_assoc() Function

PHP array_intersect_assoc() Function is used to find the intersection of two or more arrays. It is similar to thearray_intersect(). It is also used to compare the values of two or more given arrays and returns the matches. It can accept any number of arrays as arguments greater than or equal to two. It returns another array that contains the intersection of all the input arrays.

Syntax:

array_intersect_assoc($array1,$array2,$array3, ...);

Parameter Values

Parameter Description
array1 This is a required parameter. This parameterdefines the array that the others will be compared with.
array2 This is a required parameter. This parameterdefinesan array to be compared with the first array.
array3,... This is an optional parameter. This parameterdefinesmore array to be compared with the first array.

Here is an example of array_intersect_assoc() function in PHP:

<html>
<body>
<pre>
<?php
$x=array("a"=>"1","b"=>"2","c"=>"3","d"=>"4","e"=>"5");
$y=array("a"=>"1","b"=>"2","c"=>"3");
$result=array_intersect_assoc($x,$y);
print_r($result);
?>
</pre>
</body>
</html>
Output:
Array
(
    [a] => 1
    [b] => 2
    [c] => 3
)

Example 2:

<html>
<body>
<pre>
<?php
$x=array("a"=>"1","b"=>"2","c"=>"3","d"=>"4","e"=>"5");
$y=array("a"=>"1","b"=>"2","c"=>"3","d"=>"5","e"=>6);
$result=array_intersect_assoc($x,$y);
print_r($result);
?>
</pre>
</body>
</html>
Output:
Array
(
    [a] => 1
    [b] => 2
    [c] => 3
)

No Sidebar ads