array_intersect

(PHP 4 >= 4.0.1, PHP 5, PHP 7, PHP 8)

array_intersectComputes the intersection of arrays

Description

function array_intersect(array $array, array ...$arrays): array

array_intersect() returns an array containing all the values of array that are present in all the arguments. Note that keys are preserved.

Parameters

array

The array with master values to check.

arrays

Arrays to compare values against.

Return Values

Returns an array containing all of the values in array whose values exist in all of the parameters.

Changelog

Version Description
8.0.0 This function can now be called with only one parameter. Formerly, at least two parameters have been required.

Examples

Example #1 array_intersect() example

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

The above example will output:

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

Notes

Note:

Two elements are considered equal if and only if (string) $elem1 === (string) $elem2. In words: when the string representation is the same.

See Also

add a note

User Contributed Notes 21 notes

up
176
stuart at horuskol dot co dot uk
18 years ago
A clearer example of the key preservation of this function:

<?php

$array1 = array(2, 4, 6, 8, 10, 12);
$array2 = array(1, 2, 3, 4, 5, 6);

var_dump(array_intersect($array1, $array2));
var_dump(array_intersect($array2, $array1));

?>

yields the following:

array(3) {
  [0]=> int(2)
  [1]=> int(4)
  [2]=> int(6)
}

array(3) {
  [1]=> int(2)
  [3]=> int(4)
  [5]=> int(6)
}

This makes it important to remember which way round you passed the arrays to the function if these keys are relied on later in the script.
up
78
Niels
19 years ago
Here is a array_union($a, $b):

<?php
                                        //  $a = 1 2 3 4
    $union =                            //  $b =   2   4 5 6
        array_merge(
            array_intersect($a, $b),    //         2   4
            array_diff($a, $b),         //       1   3
            array_diff($b, $a)          //               5 6
        );                              //  $u = 1 2 3 4 5 6
?>
up
6
Anonymous
5 years ago
array_intersect             use Value, not callback
array_uintersect            use Value, callback receives Value
array_intersect_key         use Key, not callback
array_intersect_ukey        use Key, callback receives Key
array_intersect_assoc       use Both, not callback
array_intersect_uassoc      use Both, callback receives Key ONLY
array_uintersect_assoc      use Both, callback receives Value ONLY
array_uintersect_uassoc     use Both, One callback receives the Key, the other receives the Value.
up
7
theking at king dot ma
4 years ago
I use array_intersect for a quick check of $_GET parameters;

<?php declare(strict_types=1)

$_params = ['cust_id','prod_id'];
$_params_check = array_intersect(array_keys($_GET),$_params);
if(count($_params_check) !== count($_params)) {
    header("HTTP/1.1 400 Bad Request");
    die();
}
?>

the file will die with a 400 error if cust_id or prod_id or both are missing from $_GET. But i could be used on $_POST and $_REQUEST as  well obviously.
up
24
Shawn Pyle
17 years ago
array_intersect handles duplicate items in arrays differently. If there are duplicates in the first array, all matching duplicates will be returned. If there are duplicates in any of the subsequent arrays they will not be returned. 

<?php
array_intersect(array(1,2,2),array(1,2,3)); //=> array(1,2,2)
array_intersect(array(1,2,3),array(1,2,2)); //=> array(1,2)
?>
up
12
yuval at visualdomains dot com
11 years ago
Using isset to achieve this, is many times faster:

<?php

 $m = range(1,1000000);
  $s = [2,4,6,8,10];

// Use array_intersect to return all $m values that are also in $s
  $tstart = microtime(true);
  print_r (array_intersect($m,$s));
  $tend = microtime(true);
  $time = $tend - $tstart;
  echo "Took $time";
  
 // Use array_flip and isset to return all $m values that are also in $s
  $tstart = microtime(true);
  $f = array_flip($s);
/* $f now looks like this:
(
    [2] => 0
    [4] => 1
    [6] => 2
    [8] => 3
    [10] => 4
)
*/
// $u will hold the intersected values
  $u = [];
  foreach ($m as $v) {
    if (isset($f[$v])) $u[] = $v;
  }
  print_r ($u);
   $tend = microtime(true);
  $time = $tend - $tstart;
  echo "Took $time";
?>

Results:

Array
(
    [1] => 2
    [3] => 4
    [5] => 6
    [7] => 8
    [9] => 10
)
Took 4.7170009613037
(
    [0] => 2
    [1] => 4
    [2] => 6
    [3] => 8
    [4] => 10
)
Took 0.056024074554443

array_intersect: 4.717
array_flip+isset: 0.056
up
10
dml at nm dot ru
15 years ago
The built-in function returns wrong result when input arrays have duplicate values.
Here is a code that works correctly:

<?php
function array_intersect_fixed($array1, $array2) {
    $result = array();
    foreach ($array1 as $val) {
      if (($key = array_search($val, $array2, TRUE))!==false) {
         $result[] = $val;
         unset($array2[$key]);
      }
    }
    return $result;
}
?>