Here is a bit of code that can do what you want, it's not too elegant, but it will get the job done.
Basically it will create a nested array where the Category ID is the array key, and Category Id, Name, as well as sub categories ('cats') are values at that level.
In the example below, $hier is the array containing the category hierarchy.
// Check each level of $hier for a key with 'head' value
// if found, add cat id under it, otherwise return false
function findPlace(&$hier, $data)
{
// check for existing position on top level
foreach ($hier as $level => $cats) {
if ($level == $data['head']) {
// the category belongs here
$hier[$level]['cats'][$data['category_id']] = array(
'id' => $data['category_id'],
'name' => $data['category']
);
return true;
}
}
// if not found, check next level down if array
foreach ($hier as $level => $cats) {
if (is_array($cats)) {
return findPlace($hier[$level], $data);
}
}
// did not find a place in hierarchy
return false;
}
// create new entry
function createPlace(&$hier, $data)
{
$hier[$data['head']]['id'] = $data['head'];
$hier[$data['head']]['cats'][$data['category_id']] = array(
'id' => $data['category_id'],
'name' => $data['category']
);
}
Then, since you expressed that you wanted to re-sort using the category name (alphabetically), you can add the following functions to do the sorting:
// case insensitive string compare for alpha sort
function compareNames($a, $b)
{
return strcasecmp($a['name'], $b['name']);
}
// sort array alphabetically by 'name' key
function sortByNameKeys(&$hier) {
foreach ($hier as $key => $sub) {
if (isset($sub['cats']) && !empty($sub['cats'])){
sortByNameKeys($hier[$key]['cats']);
uasort($hier[$key]['cats'], 'compareNames');
}
}
}
Now all you have to do to get your category hierarchy sorted out is loop through your records (categories) and call the findPlace() function, here is an example:
$hier = array(); // category hierarchy will be saved here
foreach ($records as $record) {
findPlace($hier, $record) || createPlace($hier, $record);
}
sortByNameKeys($hier); // re-sort by name, keeping hierarchy
Hope that helps!
Copy/paste this code into a php file, run it and then var_dump the $hier array to get an idea of the structure.