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

Introduction to PHP and how to build your first web application in less than 10 minutes
Mar 21, 2023 programming

Introduction to PHP and how to build your first web application in less than 10 minutes

PHP is an open source scripting language for backend development. and we are going to build our very first PHP application together. ...

7 Min Read Read More
PHP Regular Expressions
Mar 22, 2023 programming

PHP Regular Expressions

Today we are going to learn about regular expressions in PHP. A regular expression is a sequence of characters that specifies a search pattern. it's important to know how to apply them in PHP. ...

5 Min Read Read More
PHP execution time
Feb 15, 2023 programming

PHP execution time

Today we are going to talk about calculating execution time in PHP. Sometimes it’s a good idea to calculate the execution time of your functions and script as a whole. ...

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

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