0

我只想总结 1 个键,但按其他 2 个键排序:

<?php
Array
(
    [0] => Array
        (
            [week] => 1
            [info] => a
            [hours] => 4
        )

    [1] => Array
        (
            [week] => 2
            [info] => a
            [hours] => 4
        )

    [2] => Array
        (
            [week] => 2
            [info] => a
            [hours] => 4
        )
    [3] => Array
        (
            [week] => 2
            [info] => b
            [hours] => 4
        )
    [4] => Array
        (
            [week] => 2
            [info] => c
            [hours] => 4
        )
    [5] => Array
        (
            [week] => 3
            [info] => b
            [hours] => 4
        )
    [6] => Array
        (
            [week] => 3
            [info] => b
            [hours] => 4
        )
)
?>

我想总结按周排序的时间。每周都有信息。所以首先我想总结每个信息的小时数,然后按周对它们进行排序。

例子:

week:1  info:a   hours:4
week:2  info:a   hours:8
week:2  info:b   hours:4
week:2  info:c   hours:4
week:3  info:b   hours:8

如何才能做到这一点?

4

1 回答 1

1

您只需要重新排列数组:

$todisplay = array();
foreach ($weeks as $week) {
   //Does not properly check for set keys; you should do isset checks
   $todisplay[$week['week']][$week['info']] += $week['hours'];
}
ksort($todisplay);

然后你可以迭代todisplay来构建表。

于 2012-09-10T23:59:18.787 回答