That's an interesting problem, especially if you want to handle
arbitrary depths.
function nest($rows=array())
{
$result = array();
for($i = 0; $i < count($rows); $i++)
{
$last = count($result) - 1; // for convenience
if(empty($result) || $result[$last]['depth'] ==
$rows[$i]['depth'])
{
$result[] = $rows[$i];
}
else if ($rows[$i]['depth'] > $result[$last]['depth'])
{
$result[$last][] = nest(array_slice($rows, $i));
$count = count($result[$last][0]);
foreach($result[$last][0] as $arr)
{
if(isset($arr['sub'])) $count += $arr['sub'];
}
$result[$last]['sub'] = $count;
// move past the rows the recursive call has already
handled
$i = $i + $count - 1;
}
else
{
return $result;
}
}
return $result;
}
$menu = nest($data);
Had to add an extra element 'sub' to some of the arrays to keep track
of the count which isn't really ideal but seems to work fine.
Undoubtedly there are ways to get around that.
I notice your post is a few days old, how did you manage to solve this?
Rory
--~--~---------~--~----~------------~-------~--~----~
NZ PHP Users Group: http://groups.google.com/group/nzphpug
To post, send email to [email protected]
To unsubscribe, send email to
[EMAIL PROTECTED]
-~----------~----~----~----~------~----~------~--~---