PHP过滤器和sum数组值

时间:2013-01-30 11:26:25

标签: php arrays

我有一个带有以下数组的php脚本(日期,任务,演员,hh:mm)。

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
)

我想创建一个过滤器,列出任务并总结每个任务的时间值,例如:

学习PHP(< -selected option)

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

实现这一目标的方法是什么? 任何建议或后续步骤都非常受欢迎。

1 个答案:

答案 0 :(得分:0)

如果你有一个多维数组,这将更容易过滤。这样的东西虽然适用于当前的结构:

$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;