php - 从数据库记录范围中提取高值和低值的算法

标签 php

我有一个这样的数据结构:

 array (size=10)
   1 => 
    array (size=2)
     'fdate' => string '11/05/12' (length=8)
     'ftime' => string '11:13' (length=5)
   2 => 
    array (size=2)
    'fdate' => string '11/05/12' (length=8)
    'ftime' => string '11:13' (length=5)
   3 => 
    array (size=2)
     'fdate' => string '11/05/12' (length=8)
     'ftime' => string '11:50' (length=5)
  4 => 
    array (size=2)
     'fdate' => string '11/05/12' (length=8)
     'ftime' => string '11:51' (length=5)
  5 => 
    array (size=2)
     'fdate' => string '11/07/12' (length=8)
     'ftime' => string '09:11' (length=5)
  6 => 
    array (size=2)
     'fdate' => string '11/07/12' (length=8)
     'ftime' => string '09:12' (length=5)
  7 => 
    array (size=2)
     'fdate' => string '11/07/12' (length=8)
     'ftime' => string '10:29' (length=5)
  8 => 
    array (size=2)
     'fdate' => string '11/08/12' (length=8)
     'ftime' => string '11:06' (length=5)
  9 => 
    array (size=2)
    'fdate' => string '11/08/12' (length=8)
    'ftime' => string '11:06' (length=5)
  10 => 
    array (size=2)
    'fdate' => string '11/08/12' (length=8)
    'ftime' => string '11:07' (length=5)

在本例中,我需要评估并最终得到每个日期的第一个和最后一个 ftime 值

 fdate     ftime1 ftime2
 11/05/12, 11:13, 11:51
 11/07/12, 09:11, 10:29
 11/08/12, 11:06, 11:07

我有一个这样的循环:

 $struct = array();
 $i = 1;
 foreach($recs as $row){            
   $struct[$i]['fdate'] = $row->fdate;// store fdate
   $struct[$i]['ftime1'] = $row->ftime;// store initial ftime
   $currentfdate = $row->fdate; // use $currentfdate in comparison to $row->fdate

   if($currentfdate <> $row->fdate){
      $struct[$i]['ftime2'] = $row->ftime;// store last ftime
   }

   $i++;
 }

我不确定如何确定 fdate 何时更改以触发 ftime2 的存储。

最佳答案

你可以试试这个:

$temp = array();
foreach($recs as $row){            
   $temp[$row->fdate][]=$row->ftime;
}

foreach($temp as $date=>$value){
  echo $date.'-'.current($value).'-'.end($value);
}

关于php - 从数据库记录范围中提取高值和低值的算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13308948/

相关文章:

php - 严格标准:响应声明::toXML() 错误

php - 如何跨多个页面传递多个变量?

php - Facebook API : How to get user blocked list

javascript - 通过在 codeigniter 中使用 jquery load 方法发布序列化数据

javascript - renderWith 输出在控制台中可见,但不更新浏览器

php - Woocommerce - 结帐页面上的订单审核总数未刷新

php echo 无法打印

php - 如何从命令行执行 PHP 代码?

php - 将不带列标题的 .csv 文件上传到 PHP MySQL 数据库

php - SSL 加密 ZF2 PDO 连接到 Mysql