[英]PHP filter and sum array values
I've got a php script with following array (date,task,actor,hh:mm). 我有一个具有以下数组(日期,任务,演员,hh:mm)的php脚本。
Array ( [0] => 2013-01-29|Making movies|Laurel|07:30 [1] => 2013-01-29|Making movies|Hardy|00:30 [2] => 2013-01-29|Learning PHP|Hardy|07:00 [3] => 2013-01-29|Singing autographs|Keaton|07:30 [4] => 2013-01-29|Making movies|Lloyd|07:30 [5] => 2013-01-28|Learning PHP|Laurel|07:30 [6] => 2013-01-28|Making movies|Hardy|07:30 [7] => 2013-01-28|Learning PHP|Keaton|07:30 [8] => 2013-01-28|Making movies|Lloyd|07:30 [9] => 2013-01-27|Learning PHP|Laurel|05:30 [10] => 2013-01-27|Making movies|Laurel|02:30 [11] => 2013-01-27|Learning PHP|Hardy|07:30 [12] => 2013-01-27|Making movies|Keaton|07:30 [13] => 2013-01-27|Making movies|Lloyd|07:30 )
I'd like to create a filter that lists the tasks and sums the time values of each task, for example: 我想创建一个过滤器,该过滤器列出任务并汇总每个任务的时间值,例如:
Learning PHP (<-selected option) 学习PHP(<选择的选项)
2013-01-29 Hardy 07:00
2013-01-28 Laurel 07:30
2013-01-28 Keaton 07:30
2013-01-27 Laurel 05:30
2013-01-27 Hardy 07:30
=======================
TOTAL 35:00
What would be the way to make this happen? 如何做到这一点? Any suggestions or next steps are more than welcome.
任何建议或后续步骤都将受到欢迎。
This would be easier to filter if you had a multidimensional array. 如果您有多维数组,这将更容易过滤。 Something like this would work for the current structure though:
像这样的东西对于当前结构仍然适用:
$task = "Learning PHP";
foreach($arr as $k=>$v) {
$pieces = explode("|", $v);
if($pieces[1]==$task) {
$total += (int) str_replace(':','',$pieces[3]);
echo $pieces[0] . " " . $pieces[2] . " " . $pieces[3];
}
}
echo "Total: " . $total;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.