Free cookie consent management tool by TermsFeed Generator PHP Array Intersect | Amir Kamizi
AMIR KAMIZI
Home Blog Courses Books Newsletter Store Membership Buy me a coffee
PHP Array Intersect

PHP Array Intersect

Last Updated on Feb 14, 2023

Introduction

If you want to find the items in your array that are present in the other arrays, array intersect is your friend.

Here we have 3 arrays:

$people = [
   'php'      => 'amir',
   'feedhive' => 'simon',
   'twitter'  => 'pratham',
   'docker'   => 'francesco',
   'web3'     => 'oliver',
   'saas'     => 'simon'
];
$peopleA = [
   'php'      => 'amir',
   'feedhive' => 'simon',
   'twitter'  => 'pratham',
   'php'      => 'amir',
];
$peopleB = [
   'twitter'  => 'pratham',
   'docker'   => 'francesco',
   'web3'     => 'oliver',
   'saas'     => 'simon',
   'php'      => 'amir updated',
];

Array Intersect

we want to find the items in people array that are present in peopleA and peopleB arrays.

There are 3 different array intersect functions and we should choose them based on what we want to compare.

array_intersect if we want to compare based on values

array_intersect_key if we want to compare based on keys

array_intersect_assoc if we want to compare based on keys and values

Let’s see what is the result of each on of them

$people = [
   'php'      => 'amir',
   'feedhive' => 'simon',
   'twitter'  => 'pratham',
   'docker'   => 'francesco',
   'web3'     => 'oliver',
   'saas'     => 'simon'
];
$peopleA = [
   'php'      => 'amir',
   'feedhive' => 'simon',
   'twitter'  => 'pratham',
   'php'      => 'amir',
];
$peopleB = [
   'twitter'  => 'pratham',
   'docker'   => 'francesco',
   'web3'     => 'oliver',
   'saas'     => 'simon',
   'php'      => 'amir updated',
];
array_intersect($people,$peopleA,$peopleB);
//Array
//(
//    [feedhive] => simon
//    [twitter] => pratham
//    [saas] => simon
//)

array_intersect_key($people,$peopleA,$peopleB);
//Array
//(
//    [php] => amir
//    [twitter] => pratham
//)

array_intersect_assoc($people,$peopleA,$peopleB);
//Array
//(
//    [twitter] => pratham
//)

Conclusion

Now you know about array intersect function in PHP.

I recommend you to open a PHP files and try to define multiple arrays. then try to find the values that intersect between the arrays.

If you have any suggestions, questions, or opinions, please contact me. I’m looking forward to hearing from you!

Key takeaways

  • array intersect function in PHP
  • array intersect based on keys
  • array intersect based on values
  • array intersect based on keys and values

Category: programming

Tags: #php #array

Join the Newsletter

Subscribe to get my latest content by email.

I won't send you spam. Unsubscribe at any time.

Related Posts

PHP random number
Feb 15, 2023 programming

PHP random number

Today we are going to talk about generating random numbers in PHP. Knowing how to generate a random number is more useful than you think. you can select random users and much more. ...

3 Min Read Read More
Creating a Simple Template Engine like Laravel Blade in PHP
Aug 21, 2023 programming

Creating a Simple Template Engine like Laravel Blade in PHP

Today we are going to learn how to build a simple template engine like Laravel's Blade. it's a great learning opportunity to understand how popular engines like Blade might work under the hood. ...

4 Min Read Read More
New PHP Tools You’ve Probably Never Heard Of
Jul 15, 2025 programming

New PHP Tools You’ve Probably Never Heard Of

The PHP ecosystem is constantly evolving, with fresh packages, libraries, and tools emerging that aim to solve old problems in new ways. There’s a growing landscape of lesser-known tools quietly gaining traction within the PHP community. ...

10 Min Read Read More
Vue.js Beginner to Advanced 2025: Complete Guide
Jul 21, 2025 programming

Vue.js Beginner to Advanced 2025: Complete Guide

Vue.js has evolved into one of the most popular JavaScript frameworks for building dynamic web applications. it continues to strike a balance between simplicity and capability, offering a progressive approach to front-end development. ...

33 Min Read Read More

Recommended Courses

Introduction to Machine Learning in PHP

Introduction to Machine Learning in PHP

Learn to Build Different Machine Learning Models Easily ...

PHP Tutorial Beginner to Advanced

PHP Tutorial Beginner to Advanced

Learn everything you need to start a successful career as a PHP developer ...