Converting Flattened Data to Nested

I currently am doing an aggregate query that groups by two fields and sums a third field.  The result is the following:

[period:1,week:1,total_hours:2]; [period:5,week:1,total_hours:3]; [period:2,week:2,total_hours:5]; [period:6,week:2,total_hours:8]; [period:12,week:3,total_hours:4]; [period:3,week:4,total_hours:2]

I am trying to pass this data into a grid but with one row per "week" value with an array of period / hour values as below:

[week:1,periods:[[period:1,total_hours:2],[period:5,total_hours:3]]]; [week:2,periods:[[period:2,total_hours:5],[period:6,total_hours:8]]]; [week:3,periods:[[period:12,total_hours:4]]]; [week:4,periods:[[period:3,total_hours:2]]]

- Can this be done in the queryRecordType itself?

- Which array methods would be best suited to handle this?

- Should I be using foreach to do this?

  Discussion posts and replies are publicly visible