比较日期与循环中的上一个日期

时间:2015-02-25 01:01:29

标签: php

我循环查询包含值为整数和日期时间的查询。我想比较循环中的每一行,如果已经存在具有相同日期(NOT TIME)的记录,它应该删除数组中的前一个记录/对象。我怎样才能做到这一点?

foreach ($balanceQuery as $row) 
{ 

    $value = $row->balance;


    $datetime = strtotime($row->post_date) * 1000;
    $data[] = "[$datetime, $value]";
}

1 个答案:

答案 0 :(得分:0)

请让我试试。

$tempDates = array();
$tempDelete = array(); 
foreach ($balanceQuery as $key=>$row){ 
    $value = $row->balance;
    $datetime = date("Y-m-d", strtotime($row->post_date)); // make format just date
    if(in_array($datetime, $tempDates)){ // if date is exist in $tempDates
        $keys = array_keys($tempDates, $datetime); // get the keys
        if(!empty($keys)){
             $tempDelete[] = $keys[count($keys)-1]; // put the last exist date
        }
    }
    $tempDates[$key] = $datetime; // store to array with $key array
}

// now looping for delete

foreach($tempDelete as $del){
    unset($balanceQuery[$del]);
}