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 array map
Feb 14, 2023 programming

PHP array map

Today we are going to learn about array map function in PHP. It lets you apply a function to the elements of the given arrays and returns an array with changed elements. ...

4 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
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
How to Split an Excel File into Multiple Files Using PHP
May 07, 2024 programming

How to Split an Excel File into Multiple Files Using PHP

Sometimes you might have an excel files that you need to split. splitting the file into multiple files with PHP can be easily done. ...

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