AppDividend
Latest Code Tutorials

PHP Array Intersect Example | Array_intersect() Function Tutorial

1

PHP array_intersect() is an inbuilt function that compares the values of two arrays and returns the matches. The array_intersect() function compares the values of two or more arrays, and return the array that contains the entries from array1 that are present in the array2, array3, etc.

PHP Array Intersect Example

The array_intersect() built-in function of PHP is used to compute an intersection of two or more arrays. PHP array_intersect() function is used to compare the values of two or more arrays and returns the matches.

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

The function prints only those elements of the first array that are present in all other arrays.

Syntax

See the below syntax.

array_intersect(array1,array2,array3...);

The array1 parameter is required and the array to compare from.

The array2 parameter is required and the array to compare from.

The array3 parameter is optional and more arrays to compare against.

See the below example.

<?php

// app.php

$arrA = [
  'khal drogo' => 'Jason Momoa',
  'Khaleesi' => 'Emilia Clarke',
  'Jon Snow' => 'Kit harington'
];
$arrB = [
  'khal drogo' => 'Jason Momoa',
  'Ygritte' => 'Rose Leslie',
  'Jon Snow' => 'Kit harington'
];
$result=array_intersect($arrA,$arrB);
print_r($result);

See the below example.

PHP Array Intersect Example

Now, let’s compare the three arrays. See the following code.

<?php

// app.php

$arrA = [
  'khal drogo' => 'Jason Momoa',
  'Khaleesi' => 'Emilia Clarke',
  'Jon Snow' => 'Kit harington'
];
$arrB = [
  'khal drogo' => 'Jason Momoa',
  'Ygritte' => 'Rose Leslie',
  'Jon Snow' => 'Kit harington'
];
$arrC = [
  'Jorah Mormont' => 'Iain Glenn',
  'Ygritte' => 'Rose Leslie',
  'Jon Snow' => 'Kit harington'
];
$result = array_intersect($arrA, $arrB, $arrC);
print_r($result);

See the below output.

PHP array_intersect() Function Tutorial

See the above code, we have compared the three arrays, and it will output only the standard value between those arrays. If no element matches then, a NULL array is returned.

Empty or Null return

Let’s see an example.

<?php

// app.php

$arrA = [
  'khal drogo' => 'Jason Momoa',
  'Khaleesi' => 'Emilia Clarke',
  'Jon Snow' => 'Kit harington'
];
$arrB = [
  'khal drogo' => 'Jason Momoa',
  'Ygritte' => 'Rose Leslie',
  'Jon Snow' => 'Kit harington'
];
$arrC = [
  'Jorah Mormont' => 'Iain Glenn',
  'Ygritte' => 'Rose Leslie',
  'Tormund' => 'Kristofer Hivju'
];
$result = array_intersect($arrA, $arrB, $arrC);
print_r($result);

In the above code, there are three arrays. If any two arrays have some of the same values, but the third array does not contain the common value then also, it returns empty or null.

For example, in the arrA and arrB, there is a common key/value called Jon Snow => Kit Harington. But, in the third array arrC, that value is not present, and we are comparing all the three values.

That is why it will return an empty array. See the below output.

PHP Array Intersect Tutorial

So, first, it checks for the first and second array and finds the match values and then compares the third array and if the intersection between those arrays output common values then it will print otherwise it will return an empty value.

Handle Duplicate Values

The array_intersect handles the duplicate items in the arrays differently. If there are duplicate values in the first array, all the matching the duplicate values will be returned. If there are duplicate values in any of the subsequent arrays then, they will not be returned. See the below code.

<?php

// app.php

$arrA = [21, 19, 21, 46];
$arrB = [19, 21, 29];

$result = array_intersect($arrA, $arrB);
print_r($result);

See the output.

Handle Duplicate Values in php array_intersect function

Now, let’s take a scenario where the first array does not contain duplicate values but the second array contains the duplicate values and see the output.

<?php

// app.php

$arrA =  [19, 21, 29];
$arrB = [21, 19, 21, 46];

$result = array_intersect($arrA, $arrB);
print_r($result);

See the output.

Handle Duplicate Values

Finally, PHP Array Intersect Example | PHP array_intersect() Function Tutorial is over.

1 Comment
  1. Bablu Singh Kulyal says

    how to get same value
    Array ( [0] => 13 [1] => 14 [2] => 15 [3] => 16 [4] => 17 [5] => 18 )
    Array ( [0] => 9 [1] => 10 [2] => 16 [3] => 17 )

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.