PHP array_intersect Example


PHP array_intersect() function is used to find intersection between arrays. This function can accept multiple arguments to compare. After comparison, this function returns an array of intersecting elements that are commonly available in all the given input arrays.

array_intersect() is the reverse of PHP array_diff() which returns the elements that are not in the arguments.


PHP array_intersect Example

In this example, we are going to pass four arrays as parameters.

This program prints resultant array containing values that are commonly present in all the four input arrays. The output is,

Other Array Intersection Functions in PHP

PHP includes some other functions to calculate intersected elements among input arrays. These functions are,

  • array_intersect_key() – returns intersecting elements with respect to keys.
  • array_interset_assoc() – returns intersecting elements with respect to keys and values.
  • array_intersect_ukey() – returns intersecting elements on the basis of custom functions provided an algorithm for the intersection.

Leave A Reply

Your email address will not be published.