Search code examples
phparraysmultidimensional-arrayfilteringintersection

Find the common values in an array of arrays


I have an array of arrays.

$a = [
    [1, 2, 3],
    [2, 3, 4],
    [2, 4, 5]
];

How can I find common element which exist in all array?

In the above sample data, I'd expect to have a result array containing the value 2 because it is the only value that occurs in all rows.


Solution

  • You can avoid foreach loop by

    call_user_func_array('array_intersect',$a);