Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have a multidimensional array in the form of;

Array
(
    [0] => Array
        (
            [DATE] => 05-19-2011
            [PERSON] => Bob Hope
            [ITEM] => Holiday
            [DURATION] => 04:00
        )
    [1] => Array
        (
            [DATE] => 05-19-2011
            [PERSON] => Bob Hope
            [ITEM] => Work
            [DURATION] => 05:00
        )
    [2] => Array
        (
            [DATE] => 05-19-2011
            [PERSON] => Bob Hope
            [ITEM] => Holiday
            [DURATION] => 06:00
        )
    [3] => Array
        (
            [DATE] => 05-19-2011
            [PERSON] => Bob Hope
            [ITEM] => Work
            [DURATION] => 05:00
        )
    [4] => Array
        (
            [DATE] => 05-19-2011
            [PERSON] => Joe Bloggs
            [ITEM] => Holiday
            [DURATION] => 02:00
        )
    [5] => Array
        (
            [DATE] => 05-19-2011
            [PERSON] => Joe Bloggs
            [ITEM] => Work
            [DURATION] => 03:00
        )
    [6] => Array
        (
            [DATE] => 05-19-2011
            [PERSON] => Joe Bloggs
            [ITEM] => Holiday
            [DURATION] => 03:00
        )
    [7] => Array
        (
            [DATE] => 05-19-2011
            [PERSON] => Joe Bloggs
            [ITEM] => Work
            [DURATION] => 02:00
        )
)

And want to collate the results, totalling the holiday and work for each person so that I can output something in the form of;

Array
(
    [0] => Array
        (
            [DATE] => 05-19-2011
            [PERSON] => Bob Hope
            [ITEM] => Holiday
            [DURATION] => 10:00
        )
    [1] => Array
        (
            [DATE] => 05-19-2011
            [PERSON] => Bob Hope
            [ITEM] => Work
            [DURATION] => 10:00
        )
    [2] => Array
        (
            [DATE] => 05-19-2011
            [PERSON] => Joe Bloggs
            [ITEM] => Holiday
            [DURATION] => 05:00
        )
    [3] => Array
        (
            [DATE] => 05-19-2011
            [PERSON] => Joe Bloggs
            [ITEM] => Work
            [DURATION] => 05:00
        )
)

Any ideas? Any and all help appreciated.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
226 views
Welcome To Ask or Share your Answers For Others

1 Answer

Where's the data coming from? Generally, such data comes from a database, in which case the following statement would do exactly what you want:

SELECT 
   SUM(DURATION),
   ITEM,
   PERSON
FROM
   yourtable
GROUP BY 
   PERSON, 
   ITEM;

Edit: If you want to group them by items too, just declare that.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...