PHP array intersect functions help identify common elements between arrays. They are commonly used when comparing datasets, filtering values, or matching keys across arrays.
array_intersect() compares array valuesarray_intersect_key() compares array keysarray_intersect() returns values present in all arrays, while array_intersect_key() returns key-value pairs with matching keys.
// Find common values between two arrays
<?php
$array1 = array("a" => "apple", "b" => "banana", "c" => "cherry");
$array2 = array("d" => "date", "b" => "banana", "e" => "elderberry");
$common = array_intersect($array1, $array2);
print_r($common);
?>
The value banana exists in both arrays, so it is returned with its original key.
// Find matching keys between two arrays
<?php
$array1 = array("a" => "apple", "b" => "banana", "c" => "cherry");
$array2 = array("b" => "blueberry", "c" => "citrus", "d" => "date");
$result = array_intersect_key($array1, $array2);
print_r($result);
?>
Keys b and c exist in both arrays, so their values from the first array are returned.
// Compare multiple arrays to find common values
<?php
$a1 = array("apple", "banana", "cherry");
$a2 = array("banana", "cherry", "date");
$a3 = array("cherry", "date", "elderberry");
$result = array_intersect($a1, $a2, $a3);
print_r($result);
?>
array_values() if reindexing is required