在数组PHP中计算日期

时间:2016-05-28 16:54:31

标签: php arrays

我有这个数组:

Array (
[0] => Array ( [x] => 2016-04-19 ) 
[1] => Array ( [x] => 2016-05-25 ) 
[2] => Array ( [x] => 2016-05-26 ) 
[3] => Array ( [x] => 2016-05-27 ) 
[4] => Array ( [x] => 2016-05-28 ) 
[5] => Array ( [x] => 2016-05-29 ) 
[6] => Array ( [x] => 2016-05-29 ) 
[7] => Array ( [x] => 2016-06-02 ) 
[8] => Array ( [x] => 2016-06-03 ) 
[9] => Array ( [x] => 2016-06-07 ) 
[10] => Array ( [x] => 2016-06-10 ) 
[11] => Array ( [x] => 2016-06-17 ) 
[12] => Array ( [x] => 2016-06-24 ) )

我正在尝试计算重复的天数,并在变量中获取此数字。例如,我们可以看到有两个相同的日期:

  

2016年5月29日

我已经尝试了array_count_values(),但它说它只能计算字符串和整数。这是正确的,因为这是一个Date变量。这是代码:

$eventDates = $object->get("date");
$result = $eventDates->format("Y-m-d");
$data[] = array("x"=>$result);

知道如何计算它们吗?

3 个答案:

答案 0 :(得分:2)

使用array_map您可以将日期转换为字符串,然后您可以使用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 
)

然后您可以使用它来确定重复项。

(如果日期包含您想要忽略的时间元素,这也很有用。)

答案 1 :(得分:2)

获取所需的列并计算值:

$count = array_count_values(array_column($array, 'x'));

然后,要查找重复项,请获取计数1和计数大于1之间的差异:

$dupes = array_diff(array_flip($count), array_keys($count, 1));

答案 2 :(得分:0)

你说过只计算重复吗?好吧,您可以循环遍历给定日期的数组,并将重复项捆绑到一个新数组中,然后得到结果数组的计数,如下所示:

let text = "he said hello and then ran away"    // This is taken from the "activity" category
// Dictionary associating keywords to categories
let categoryRules = ["hi" : "greeting", "hello" : "greeting", "jogging" : "activity", "joy" : "feeling"]

let keywords = Array(categoryRules.keys)
// Make out of text an Array of words.
let textWordArray = text.lowercaseString.characters.split{$0 == " "}.map(String.init)

// I SHOULDN'T HAVE TO GO THROUGH THE KEYS ASSOCIATED WITH "ACTIVITY" BECAUSE TEXT IS ALREADY IN IT.
for keyword in keywords {     
    // If text contains the rule
    if let index = textWordArray.indexOf(keyword) {
        // Get the associated category
        if let category = categoryRules[keyword] {
            print("The text should fall into the category of \(category)")
            break
        }
    }
}

VAR_DUMP的结果

    <?php


        $arrHolder      = array();
        $arrDuplicates  = array();
        $arrDateList    = array(
            "date1"     => '2016-04-19',
            "date2"     => '2016-05-25',
            "date3"     => '2016-05-26',
            "date4"     => '2016-05-27',
            "date5"     => '2016-05-28',
            "date6"     => '2016-05-29',   //<== DUPLICATE DATE : 1
            "date7"     => '2016-05-29',   //<== DUPLICATE DATE : 2
            "date8"     => '2016-06-02',
            "date9"     => '2016-06-03',
            "date10"    => '2016-06-07',
            "date11"    => '2016-06-10',
            "date12"    => '2016-06-17',
            "date13"    => '2016-06-24',
            "date14"    => '2016-05-29',   //<== DUPLICATE DATE : 3
            "date15"    => '2016-05-29',   //<== DUPLICATE DATE : 4
        );

    // LOOP THROUGH ALL THE GIVEN ARRAYS CHECKING IF ANY OF THEM ALREADY EXIST
    // IF NOT, WE JUST PUSH THE DATE TO $arrHolder ARRAY
    // OTHERWISE WE PERFORM MORE CHECK AND PUSH IT INTO A MULTI-DIMENSIONAL ARRAY: $arrDuplicates.  
    foreach($arrDateList as $key=>$date){
        if(!in_array($date, $arrHolder)){
            $arrHolder[$key]                    = $date;
        }else{
            if(!array_key_exists($date, $arrDuplicates)){
                // IF THIS KEY EXIST, IT MEANS THAT THIS IS THE 2ND INSTANCE
                // SO WE ASSIGN A COUNT OF 2 TO IT.
                $arrDuplicates[$date]           = array($key=>$date, "count"=>2);
            }else{
                $arrDuplicates[$date]["count"]  =  intval($arrDuplicates[$date]["count"])  + 1;
            }
        }
    }

    var_dump($arrDuplicates);