You are discussing a sort of recursion then. Recursion is the act of a function calling itself. It is most often applicable to, surprise, recursive structures. It isn't necessarily the fastest approach, and it can have difficulty with scalability.
Here's an example:
<?php
$selectedDragon = 5;
$dragons = array(
1 => array('mom' => null, 'dad' => null),
2 => array('mom' => null, 'dad' => null),
3 => array('mom' => null, 'dad' => null),
4 => array('mom' => 1, 'dad' => 2),
5 => array('mom' => 4, 'dad' => 3)
);
?>
<h1>Lineage of Dragon <?=$selectedDragon ?></h1>
<table>
<tr> <th>Dragon</th> <th>Mother</th> <th>Father</th> </tr>
<?php getParents($selectedDragon, $dragons); ?>
</table>
<?php
function getParents($dragonId, $dragons)
{
if (isset($dragons[$dragonId])) {
$dragon = $dragons[$dragonId];
$mom = (!is_null($dragon['mom'])) ? $dragon['mom'] : 'unknown';
$dad = (!is_null($dragon['dad'])) ? $dragon['dad'] : 'unknown';
printf("<tr> <td>%d</td> <td>%s</td> <td>%s</td> </tr>\n", $dragonId, $mom, $dad);
if (!is_null($dragon['mom'])) {
getParents($dragon['mom'], $dragons);
}
if (!is_null($dragon['dad'])) {
getParents($dragon['dad'], $dragons);
}
}
}
?>