Recursive functions, whoop!
Here is an example of a recursive function being used in one of my view files.
<tbody>
<?php function album_row($albums, $parent, $lvl) { ?>
<?php if(isset($albums[$parent])) foreach ($albums[$parent] as $album): ?>
<tr>
<td><?php echo form_checkbox('action_to[]', $album->id); ?></td>
<td><?php echo repeater('-- ', $lvl);?> <?php echo $album->title;?></td>
<td><?php echo $album->num_photos;?></td>
<td><?php echo date('M d, Y', $album->updated_on);?></td>
<td><?php echo anchor('photos/' . $album->slug, lang('photo_albums.view_label'), 'target="_blank"') . ' | ' .
anchor('admin/photos/manage/' . $album->id, lang('photo_albums.manage_label')) . ' | ' .
anchor('admin/photos/edit/' . $album->id, lang('photo_albums.edit_label')) . ' | ' .
anchor('admin/photos/delete/' . $album->id, lang('photo_albums.delete_label'), array('class'=>'confirm')); ?>
</td>
</tr>
<?php album_row($albums, $album->id, $lvl+1) ?>
<?php endforeach; }?>
<?php album_row($albums, 0, 0); ?>
</tbody>
Yours will be a little different as basically you want to create a function that checks if the content is an array or a string.
If its a string, echo. If its an array, call the same function again.