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
) 
3

1 Answer 1

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!

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.