gpt4 book ai didi

php - 从 db recs 范围中提取高值和低值的算法

转载 作者:行者123 更新时间:2023-12-04 05:24:53 25 4
gpt4 key购买 nike

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

 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 - 从 db recs 范围中提取高值和低值的算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13308948/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com