gpt4 book ai didi

php - 按字段对对象数组进行排序

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

我想按“日期”字段对以下选项卡进行排序并计算“临时”字段的总和:

0 =>     array (size=3)      'id_cra' => string '4586' (length=4)      'temps' => string '03:00:00' (length=8)      'date' => string '2013-06-03' (length=10) 
1 => array (size=3) 'id_cra' => string '4587' (length=4) 'temps' => string '03:00:00' (length=8) 'date' => string '2013-06-06' (length=10)
2 => array (size=3) 'id_cra' => string '4588' (length=4) 'temps' => string '03:00:00' (length=8) 'date' => string '2013-06-05' (length=10)
3 => array (size=3) 'id_cra' => string '4589' (length=4) 'temps' => string '03:00:00' (length=8) 'date' => string '2013-06-06' (length=10)
4 => array (size=3) 'id_cra' => string '4590' (length=4) 'temps' => string '03:00:00' (length=8) 'date' => string '2013-06-07' (length=10)
5 => array (size=3) 'id_cra' => string '4591' (length=4) 'temps' => string '02:00:00' (length=8) 'date' => string '2013-06-03' (length=10)
6 => array (size=3) 'id_cra' => string '4592' (length=4) 'temps' => string '03:30:00' (length=8) 'date' => string '2013-06-03' (length=10)

预期结果:

日期 = >“临时”字段的总和
2013-06-03 =>08:30:00
2013-06-06 =>06:00:00

等等

PS:抱歉问了一些对你来说可能很简单的问题,我已经在网上查过了(例如这里: Sort array of objects by object fields)但我不明白答案

最佳答案

我没有尝试,但它必须是这样的:

$arr = [your array data]; // your array which has to be sorted

usort($arr, 'sort_array');

function sort_array($a, $b) {

$timeA = strtotime($a['date']);
$timeB = strtotime($b['date']);

if($timeA == $timeB) {
return 0;
}
return $timeA < $timeB ? -1 : 1;
}

在对数组进行排序后,计算总和将是一个循环,您必须按日期“分组”并对“临时”求和

关于php - 按字段对对象数组进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17215136/

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