Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
237 views
in Technique[技术] by (71.8m points)

php - Collating multidimensional array values

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

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

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...