PHP - Function array_intersect()


Advertisements


Syntax

array array_intersect ( array $array1, array $array2 [, array $array3 ...] );

Definition and Usage

Returns an array containing all the values of array1 that are present in all the arguments. Note that keys are preserved.

Parameters

Sr.No Parameter & Description
1

array1

Required. The first array is the array that the others will be compared with.

2

array2

Required. An array to be compared with the first array

3

array3

Optional. An array to be compared with the first array

Return Values

Returns an array containing all the entries from array1 that are not present in any of the other arrays.

Example

Try out following example −

<?php
   $array1 = array("a" => "green", "red", "blue");
   $array2 = array("b" => "green", "yellow", "red");
   $result = array_intersect($array1, $array2);
   
   print_r($result);
?> 

This will produce the following result −

Array (
   [a] => green
   [0] => red
)

php_function_reference.htm

Advertisements