用于从db recs范围中提取高值和低值的算法

时间:2012-11-09 13:25:40

标签: 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的存储。

2 个答案:

答案 0 :(得分:2)

你可以试试这个:

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

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

答案 1 :(得分:1)

这个解决方案有点幼稚,因为它假设您的记录集已经正确排序(意味着相同的日期彼此相邻,并且它们的时间按升序顺序排列):

<?php

$records = array(
    array( 'fdate' => '11/05/12', 'ftime' => '11:13' ),
    array( 'fdate' => '11/05/12', 'ftime' => '11:13' ),
    array( 'fdate' => '11/05/12', 'ftime' => '11:50' ),
    array( 'fdate' => '11/05/12', 'ftime' => '11:51' ),
    array( 'fdate' => '11/07/12', 'ftime' => '09:11' ),
    array( 'fdate' => '11/07/12', 'ftime' => '09:12' ),
    array( 'fdate' => '11/07/12', 'ftime' => '10:29' ),
    array( 'fdate' => '11/08/12', 'ftime' => '11:06' ),
    array( 'fdate' => '11/08/12', 'ftime' => '11:06' ),
    array( 'fdate' => '11/08/12', 'ftime' => '11:07' )
);

$struct = array();
$currentDate = null;
$i = 0; // if you want zero-indexed result, change this to -1
foreach( $records as $record )
{
    if( $currentDate <> $record[ 'fdate' ] )
    {
        $i++;
        $struct[ $i ] = array(
            'fdate' => $record[ 'fdate' ],
            'ftime1' => $record[ 'ftime' ]
        );
        $currentDate = $record[ 'fdate' ];
    }
    else if( $struct[ $i ][ 'ftime1' ] <> $record[ 'ftime' ] )
    {
        $struct[ $i ][ 'ftime2' ] = $record[ 'ftime' ];
    }
}

var_dump( $struct );

(将数组表示法替换为对象表示法以满足您的需要。)