Array intersection

suggest change

The array_intersect function will return an array of values that exist in all arrays that were passed to this function.

$array_one = ['one', 'two', 'three'];
$array_two = ['two', 'three', 'four'];
$array_three = ['two', 'three'];

$intersect = array_intersect($array_one, $array_two, $array_three);
// $intersect contains ['two', 'three']

Array keys are preserved. Indexes from the original arrays are not.

array_intersect only check the values of the arrays. array_intersect_assoc function will return intersection of arrays with keys.

$array_one = [1 => 'one',2 => 'two',3 => 'three'];
$array_two = [1 => 'one', 2 => 'two', 3 => 'two', 4 => 'three'];
$array_three = [1 => 'one', 2 => 'two'];

$intersect = array_intersect_assoc($array_one, $array_two, $array_three);
// $intersect contains [1 =>'one',2 => 'two']

array_intersect_key function only check the intersection of keys. It will returns keys exist in all arrays.

$array_one = [1 => 'one',2 => 'two',3 => 'three'];
$array_two = [1 => 'one', 2 => 'two', 3 => 'four'];
$array_three = [1 => 'one', 3 => 'five'];

$intersect = array_intersect_key($array_one, $array_two, $array_three);
// $intersect contains [1 =>'one',3 => 'three']

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents