"red","b"=>"green","c"=>"blue","d"=>"yellow"); $a2=array("e"=>"red","f"=>"green","g"=>"blue"); $result=array_intersect($a1,$a2); print_r($result); ?> ```"> "red","b"=>"green","c"=>"blue","d"=>"yellow"); $a2=array("e"=>"red","f"=>"green","g"=>"blue"); $result=array_intersect($a1,$a2); print_r($result); ?> ```" />
Easy Tutorial
❮ Func Simplexml Asxml Func Array In Array ❯

PHP array_intersect() Function

Complete PHP Array Reference Manual

Example

Compare the values of two arrays and return the intersection:

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

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

Definition and Usage

The array_intersect() function is used to compare the values of two (or more) arrays and return the intersection.

This function compares the values of two (or more) arrays and returns an array of intersection, which contains all the values from array1 that are present in all the other parameter arrays.


Syntax

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

Technical Details

Return Value: Returns an array containing all the values in the array1 that are present in all the other compared arrays (array2, array3, etc.).
PHP Version: 4.0.1+
--- ---

More Examples

Example 1

Compare the values of three arrays and return the intersection:

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

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

❮ Func Simplexml Asxml Func Array In Array ❯