• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / PHP / php array_intersect() – Find Intersection of Two or More Arrays in php

php array_intersect() – Find Intersection of Two or More Arrays in php

April 5, 2022 Leave a Comment

The php array_intersect() function allows us to find the intersection (common elements) of two or more arrays in php.

$array1 = array(5,3,8,2,1);
$array2 = array(9,3,4,2,1);

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

// Output:
Array
(
    [1] => 3
    [3] => 2
    [4] => 1
)

When working with multiple arrays, it can be useful to find the entire collection of elements which exist in all of your arrays. The intersection of two or more arrays is the collection of elements which are included in all of the array.

So, for example, if we have an array A and an array B, then the intersection of A and B is a collection of elements which are in both A and B.

We can get the intersection of two arrays in php easily.

To find all of the common elements of a list of arrays in php, we can use the php array_intersect() function.

Below is a simple example of how to use array_intersect() to find the intersection of two arrays in php.

$array1 = array(5,3,8,2,1);
$array2 = array(9,3,4,2,1);

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

// Output:
Array
(
    [1] => 3
    [3] => 2
    [4] => 1
)

Finding the Intersection of Three or More Arrays with array_intersect() in php

The php array_intersect() function can find the intersection of more than two arrays.

The examples above only found the intersection between two arrays, but we can find the intersection of three or more arrays just as easy.

Below is an example of how to use array_intersect() to find the intersection of three arrays in php.

$array1 = array(5,3,8,2,1);
$array2 = array(9,3,4,2,1);
$array3 = array(0,9,8,1,3);

print_r(array_intersect($array1,$array2, $array3));

// Output:
Array
(
    [1] => 3
    [4] => 1
)

Hopefully this article has been useful for you to learn how to use array_intersect() to find the intersection of two or more arrays in php.

Other Articles You'll Also Like:

  • 1.  php is_numeric – Check if Variable is a Number
  • 2.  Using strtolower() in php to Convert String to Lowercase
  • 3.  php asin – Find Arcsine and Inverse Sine of Number Using asin() Function
  • 4.  php preg_match_all – Get All Matches of Pattern in String
  • 5.  php max – Find Maximum Value of Array in php
  • 6.  php array_splice() – Remove, Insert, and Replace Elements in Array
  • 7.  php array_key_exists() Function – Check if Key Exists in Array
  • 8.  php is_string() Function – Check if Variable is String in php
  • 9.  php array_combine() – Create New Array Given Arrays of Keys and Values
  • 10.  Get Current Month of Date in php

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy