Count dates inside an array PHP

后端 未结 3 572
盖世英雄少女心
盖世英雄少女心 2021-01-27 07:46

I have this Array :

Array (
[0] => Array ( [x] => 2016-04-19 ) 
[1] => Array ( [x] => 2016-05-25 ) 
[2] => Array ( [x] => 2016-05-26 ) 
[3] =&g         


        
3条回答
  •  醉梦人生
    2021-01-27 08:02

    Using array_map you can convert the dates to strings, then you can use array_count_values:

    $theArray = array(array('x' => new DateTime('2016-04-19')), 
        array('x' => new DateTime('2016-04-19')),
        array('x' => new DateTime('2016-04-19')),
        array('x' => new DateTime('2016-05-19')));
    
    function formatDate($d) {
        return $d['x']->format('Y-m-d');
    }
    $results = array_map("formatDate", $theArray);
    
    print_r(array_count_values($results));
    

    .

    Array (
        [2016-04-19] => 3
        [2016-05-19] => 1 
    )
    

    Then you can use this to determine the duplicates.

    (This would also be useful if the dates contained time elements that you wanted to ignore.)

提交回复
热议问题