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 Trait
Mar 22, 2023 programming

PHP Trait

Today we are going to talk about traits in PHP. A class can extend only one class. But what if you wanted to inherit multiple classes and have their functionalities in your current class? ...

4 Min Read Read More
PHP simple REST API
Feb 15, 2023 programming

PHP simple REST API

Today we are going to create a simple REST API in PHP. we are going to do it with pure PHP and with no frameworks. you can create a RESTful API in 70 lines of code. ...

25 Min Read Read More
PHP Splat Operator
Feb 15, 2023 programming

PHP Splat Operator

Today we are going to talk about splat operator in PHP. Splat operator is when we add three dots before the name of the variable and it's way more useful than you think. ...

7 Min Read Read More
Transitioning from PHP to Python: A Comprehensive Guide for PHP Developers
Sep 10, 2024 programming

Transitioning from PHP to Python: A Comprehensive Guide for PHP Developers

While PHP and Python share several core programming concepts, transitioning between the two requires understanding the key differences in syntax, paradigms, and best practices. ...

19 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 ...