1

I have the following PHP multidimensional array:

$dates_array = array();
$dates_array[] = array('2018-08-01','2018-09-02','2018-12-03');
$dates_array[] = array('2018-12-03','2018-09-02','2018-08-01');
$dates_array[] = array('2018-12-03','2018-08-01','2018-08-02');
$dates_array[] = array('2018-01-26','2018-08-01','2018-12-03');

echo '<pre>',print_r($dates_array),'</pre>';

Which returns:

Array
(
    [0] => Array
        (
            [0] => 2018-08-01
            [1] => 2018-09-02
            [2] => 2018-12-03
        )

    [1] => Array
        (
            [0] => 2018-12-03
            [1] => 2018-09-02
            [2] => 2018-08-01
        )

    [2] => Array
        (
            [0] => 2018-12-03
            [1] => 2018-08-01
            [2] => 2018-08-02
        )

    [3] => Array
        (
            [0] => 2018-01-26
            [1] => 2018-08-01
            [2] => 2018-12-03
        )

)

I want to get only the dates which occur in all of the 4 arrays.

So for example in the 4 example arrays shown only "2018-08-01" and "2018-12-03" occurs in all 4.

I'd like to create a new array with only the values duplicated in all e.g.

Array
(
    [0] => 2018-08-01
    [1] => 2018-12-03
) 
The Bobster
  • 573
  • 4
  • 20
  • Zim84, I want to find the duplicates which occur in every array, the question you've linked is about combining array which I'm not trying to do – The Bobster Apr 04 '18 at 07:29
  • Lemme see what I can find from my phone... https://stackoverflow.com/questions/14291340/array-intersect-inside-multidimensional-array , https://stackoverflow.com/questions/24988759/how-to-get-common-values-from-4-multidimensional-arrays-using-array-intersect , https://stackoverflow.com/questions/10950577/php-array-intersect-on-multidimensional-array-with-unknown-number-of-keys – mickmackusa Apr 04 '18 at 07:29
  • Thanks mickmackusa, array_intersect is what I was looking for! – The Bobster Apr 04 '18 at 07:32

1 Answers1

2

You want to abstract a call to array_intersect() by calling call_user_func_array(), e.g.

$commondates=call_user_func_array('array_intersect', $dates_array);

Or, in context:

$dates_array = array();
$dates_array[] = array('2018-08-01','2018-09-02','2018-12-03');
$dates_array[] = array('2018-12-03','2018-09-02','2018-08-01');
$dates_array[] = array('2018-12-03','2018-08-01','2018-08-02');
$dates_array[] = array('2018-01-26','2018-08-01','2018-12-03');

$commondates=call_user_func_array('array_intersect', $dates_array);

echo '<pre>',print_r($commondates),'</pre>';

Watch out for the mismatched keys, though!