This is done by using a recursive function, and a nested array which you then traverse through. The following function could go n levels deep without issues.
<?php
function makeNav($item) {
$ret = '<li><a href="'.$item['url'].'">'.$item['name'].'</a>'.PHP_EOL;
if (isset($item['subPgs']) && is_array($item['subPgs']) && count($item['subPgs']) > 0) {
$ret .= ''.PHP_EOL;