I've got an array that looks like this:
$foo = array(
0 => array('a', 'b', 'c', 'd'),
1 => array('b', 'c', 'd'),
2 => array('b', 'd', 'f')
)
I'll refer to $foo[0]
, $foo[1]
, and $foo[2]
as sub-arrays.
I basically need to perform an array_intersect()
across all 3 sub-arrays in $foo
. The result should be:
array('b', 'd')
As all three sub-arrays had these values in common. What is the best way to do this?
Some considerations:
The array_intersect() function compares the values of two (or more) arrays, and returns the matches. This function compares the values of two or more arrays, and return an array that contains the entries from array1 that are present in array2, array3, etc.
The intersection of two arrays is a list of distinct numbers which are present in both the arrays. The numbers in the intersection can be in any order. For example. Input: A[] = {1,4,3,2,5, 8,9} , B[] = {6,3,2,7,5} Output: {3,2,5}
The in_array() function is an inbuilt function in PHP that is used to check whether a given value exists in an array or not. It returns TRUE if the given value is found in the given array, and FALSE otherwise.
$intersect = call_user_func_array('array_intersect',$foo);
Note that keys are preserved from $foo[0]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With