I have been trying to compare values of inner arrays from a multidimensional array and extract those common values into another array.
I have tried using array_intersect
along with a foreach
loop but this is not giving me result, here the number of inner arrays is dynamic and generated from a different function. Have anyone tried before comparing array elements of a multidimensional array?
My Array:
$days_filter = array(
[0] => array(
'00:00',
'01:30',
'02:30',
),
[1] => array(
'00:00',
'01:30',
'03:30',
),
[2] => array(
'00:30',
'01:30',
'02:30',
),
[3] => array(
'00:30',
'01:30',
'04:30',
),
);
$res_arr = $days_filter[0];
foreach ($days_filter as $filter) {
$res_arr = array_intersect($res_arr, $filter);
}
Expected output array:
$res_arr = array(
[0]=>'01:30'
)
because 01:30
is the common element of all inner arrays.