How to pick what array_intersect function to use in PHP?

PHP: How to pick what array_intersect function to use in PHP?

So you want to do an intersect, but not sure which PHP function to use? There are around 8 variations on the array intersection function built into PHP. Here is a guide for helping your pick which one to use!

The main array intersect functions in PHP

The basic idea of all of them is that it will return an array of all values from the first array that are also in the other arrays.

The differences between them are based on the following:

  • If the user provides the compare function (u)
  • If it will compare keys (assoc), values, or both
array_intersect()

This is the main one, I guess!

This will return an array with all values from $array1 that are present in all the others. It just compares the values, ignoring the keys

array_intersect_assoc()

Same as above, but it checks both values and keys

array_intersect_uassoc()

It checks the keys (via user supplied function) and values. The values are checked by normal comparison, but they keys are checked by a user supplied function.

array_intersect_key()

Returns array of everything from the first array which have keys in the other arrays.

array_intersect_ukey()

Same as above, but the comparison function for the keys is provided by the user.

array_uintersect()

Same as array_intersect() - but the user provides the comparison function for comparing the values

array_uintersect_assoc()

Same as array_uintersect() (above), but the user provided function checks the keys

array_uintersect_uassoc()

And in this function it is the two above combined. The user provides 2 comparison functions (one for keys, one for values), and it will check against both of those.

I've been coding professionally for years in PHP, and I still have to double check which one to use every time...

webdevetc profile pic
webdevetc

I am a 29 year old backend web developer from London, mostly focusing on PHP and Laravel lately. This (webdevetc.com) is my blog where I write about some web development topics (PHP, Laravel, Javascript, and some server stuff). contact me here.



More...


Comments and discussion about How to pick what array_intersect function to use in PHP?

Found this interesting? Maybe you want to read some more in this series?

Or see other topics in the PHP language

Or see other languages/frameworks:
PHP Laravel Composer Apache CentOS and Linux Stuff WordPress General Webdev and Programming Stuff JavaScript
Or see random questions

How to find the intersection (same values) from two arrays?

Why should you update APP_URL from from http://localhost?

How to Force www or non-www in htaccess

What is WordPress default Apache .htaccess rules?

How to find the index of a value in an array in JS?

How to find out which php.ini file PHP is using?

How does PHP compare objects?

How to redirect your website to a new domain/location in .htaccess

How to check if a user is logged in, inside a blade file?

How to output (echo/print) content in PHP