2

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

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

4

2 回答 2

2

你可以试试这个:

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

foreach($temp as $date=>$value){
  echo $date.'-'.current($value).'-'.end($value);
}
于 2012-11-09T14:58:49.360 回答
1

This solution is a bit naive, in the sense that it assumes your recordset is already ordered properly (meaning the same dates are adjacent to each other and that their times are sequentially ordered ascending):

<?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 );

(replace array notation to object notation for your needs.)

于 2012-11-09T15:09:06.737 回答