1

I have some fairly sizable results sets being returned from a MySQL DB that I sometimes need to break into smaller sets for either:

  • Adding together numbers within the subsets for subtotaling purposes; or
  • For creating a hierarchical table.

The tricky part though is that I sometimes need to divide the results set up more than once, and the number of subdivisions can change per table.
For example, I might have to break up a results set by year and then by month in order to produce subtotals for each year/month combination.

I'm trying to write a PHP function that can take the results set and the columns to split the data up on and then produce a new results set properly divided up.
For example, if I split data up by year and then month, I'd want to have the following structure returned:

array(
  'divider' => 'year',
  'sets' => array(
    [0] => array(
      'divider' => 'month',
      'sets' => array(
        [0] => array(), //Row returned from DB
        [1] => array(), //Row returned from DB
        [2] => array(), //Row returned from DB
        ...
      )
    ),
    [1] => array(
      'divider' => 'month',
      'sets' => array(
        [0] => array(), //Row returned from DB
        [1] => array(), //Row returned from DB
        [2] => array(), //Row returned from DB
        ...
      )
    )
    ...
  )
)

Is there any way to do this recursively or in a way so that regardless of how many subdivisions there are, I always get back the type of multidimensional array that I want?

Thank you.

HartleySan
  • 7,404
  • 14
  • 66
  • 119

0 Answers0