Skip to content
PHP Logo

In PHP, what are the implications of using array_chunk and array_intersect together for multidimensional arrays?

When using array_intersect on multidimensional arrays, the function only compares the values of the top-level arrays and does not consider the values of nested arrays. To properly intersect multidimensional arrays, you can first use array_map to apply array_intersect on each nested array individually. This way, you can compare the values within each nested array and get the intersection of all nested arrays.

php
// Example of intersecting multidimensional arrays
$array1 = [[1, 2, 3], [4, 5, 6], [7, 8, 9]];
$array2 = [[2, 3, 4], [5, 6, 7], [8, 9, 10]];

$intersectedArrays = array_map('array_intersect', $array1, $array2);

print_r($intersectedArrays);