Using the PHP Function Array_Uintersect
There are many built-in PHP functions that can help with array tasks like sorting, finding an element, returning the keys, combining arrays and more. There are also functions that do things like math operations, internationalization and other miscellaneous functions.
Whether you are creating an associative array in PHP or passing multi-value form fields through GET/POST vars, using the right function for each task can make your code more efficient. Rather than writing a recursive loop or an array-based solution, you can use the array_uintersect function to find the common elements of multiple arrays and return them.
The array_uintersect() function is an inbuilt PHP function that compares the values of two or more arrays and returns the matching entries. It accepts three parameters: an array, a callable and an optional initial value. The array is the first input array, the callable is a comparison function and the initial value represents the carry from the previous pass of the function.
You need to supply the callable with two arguments: the first array and a comparison function that will compare each of the elements. The comparison function will be called for each of the values in the first array and if the callable returns an integer greater than, equal to or less than 0 then that value will be matched with a value in the second array.
The final output will be an array containing all of the elements from the first input array that exist in all of the arguments. The keys of the elements are preserved so you can easily see which items are matching.