I have a list of say 40 alphabetically sorted terms I would like to split in groups of similar size, while keeping a grouping by starting letter.
The goal is to create an alphabetical list in multiple chunks with headers indicating the starting letters of each chunk, like A-D
, E-H
etc.
I thought about proceeding like that. Starting with the list:
$terms = array('Archers','Arrows','Bees' [etc...]);
Then group them by starting letter in a multidimensional array:
$terms = array(
'a' => array('Archers','Arrows'),
'b' => array('Bees'),
// [..etc..]
'z' => array('Zebras','Zebus')
);
Then re-group this multidimensional array into four groups that are about the same size. Something like that:
$termgroups = array(
// first group with e.g. 12 items
'A-C' => array(
'a' => array('Archers','Arrows'),
'b' => array('Bees')
),
// second group with e.g. 9 items
// ...etc...
);
But that would mean a lot of counting, iterating, maybe making a first try, then going over the whole stuff again.
I'm not sure how to approach this task and I have the feeling it's been done many times before – but I'm not sure how to call it.
How would you approach that?