PHP array_intersect()Functions

PHP Array ReferenceComplete PHP Array Reference Manual

Examples

Compare the values of two arraysValue, 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); ?>

Run Example »

Definition and Usage

The array_intersect() function compares the values of two (or more) arraysValue, and returns the intersection.

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


Syntax

array_intersect(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 containing all the values in the compared array (array1), and also in any other parameter arrays (array2orarray3etc.).
PHP Version: 4.0.1+


More Examples

Example 1

Compare the values of three arraysValue, 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); ?>

Run Example »


PHP Array ReferenceComplete PHP Array Reference Manual Other Extensions