array_intersect handles duplicate items in arrays differently. If there are duplicates in the first array, all matching duplicates will be returned. If there are duplicates in any of the subsequent arrays they will not be returned.

I needed to compare an array with associative keys to an array that contained some of the keys to the associative array. Basically, I just wanted to return only a few of the entries in the original array, and the keys to the entries I wanted were stored in another array. This is pretty straightforward (although complicated to explain), but I couldn't find a good function for comparing values to keys. So I wrote this relatively straightforward one:

If you want to use this function with arrays which have sometimes the same value several times, it won't be checked if they're existing in the second array as much as in the first.So I delete the value in the second array, if it's found there:

This function is able to sort an array based on another array that contains the order of occurrence. The values that are not present will be transferred into the end of the resultant.Questa funzione permette di ordinare i valori di un array ($tosort) basandosi sui valori contenuti in un secondo array ($base), i valori non trovati verranno posizionati alla fine dell'ordinamento senza un'ordine specifico.

Note... this function does not seem intuitive for doing intersection of flat arrays, in the sense that an intersection are common values between. This is an issue if you are doing a for loop over the results of an intersect function, as shown below, wherein the for loop iterates over something different depending on order.

Below is example of a function whioch I think works correctly, the output from original function, and new function.

This is also handy for testing an array for one of a series of acceptable elements. As a simple example, if you're expecting the query string to contain one of, say, user_id, order_id or item_id, to find out which one it is you could do this:

I did some trials and if you know the approximate size of the arrays then it would seem to be a lot faster to do this <?php array_intersect($smallerArray, $largerArray); ?> Where $smallerArray is the array with lesser items. I only tested this with long strings but I would imagine that it is somewhat universal.

I ran into a problem (PHP 5.6) with array_intersect(), though i had no idea it was it at the time : using it properly created an array to string conversion notice, through this type of code (and with both array variables verified) :

<?phpif(empty($var = array_intersect($array1, $array2))) {?>

Rewriting its supposed behaviour, even in a rather simple way, removed the notice. Not sure what caused the function to be unhappy but here's my code that works properly :

Here's my approach to intersection returning only the values present in all the arrays.Note that each array must not contain duplicate values.I don't know how effective it actually is, but perhaps it could help.

Intersection will start to compare from the first three colors like; "blue" to "green", "black" to "blue"; that are, the elements of the first array only, and yes, comparison order is arbitrary, but always in the same order.