I'm able to generate output as:
Cat1
SubCat1
SubCat3
However I need the output as
Cat1
Subcat1
 SubCat2
SubCat3
Well to build my menu, i use the db structure as :
site_id | parent_id    | site_name    | site_url  | Level
1       | 0            | Category 1   |example.com|   0
2       | 1            | Sub Cat 1    |example.com|   1
3       | 2            | Sub Cat 2    |example.com|   2
4       | 1            | Sub Cat 3    |example.com|   1
I'm able to generate my array as:
Array ( 
    [0] => Array ( 
        [0] => 63 
        [site_id] => 63 
        [1] => 0 
        [parent_id] => 0
        [2] => Category 1 
        [site_name] => Category 1 
        [3] => Link1 
        [site_url] => Link1 
        [4] => 0 
        [level] => 0 
        [children] => Array ( 
            [0] => Array ( 
                [0] => 66 
                [site_id] => 66 
                [1] => 63 
                [parent_id] => 63 
                [2] => Sub Cat 3 
                [site_name] => Sub Cat 3 
                [3] => 
                [site_url] => 
                [4] => 1
                [level] => 1 
                [children] => Array ( ) 
            ) 
            [1] => Array ( 
                [0] => 64
                [site_id] => 64 
                [1] => 63 
                [parent_id] => 63 
                [2] => Sub Cat 1
                [site_name] => Sub Cat 1 
                [3] => 
                [site_url] => 
                [4] => 2 
                [level] => 2
                [children] => Array ( 
                    [0] => Array ( 
                        [0] => 65 
                        [site_id] => 65 
                        [1] => 64 
                        [parent_id] => 64 
                        [2] => Sub Cat2 
                        [site_name] => Sub Cat2 
                        [3] =>
                        [site_url] => 
                        [4] => 1 
                        [level] => 1 
                        [children] => Array ( ) 
                    ) 
                ) 
            ) 
        ) 
    )
)
using the function from : https://stackoverflow.com/a/2795069/1137983. I have used only getTopCategories() and getCategories function from the post
Inorder to generate the output, I use:
{foreach from=$sitemap item=c name=sitemap}
 {if $c.level==0 }
<li><h2><a title="{$c.site_name}" href="{$c.site_url}">{$c.site_name}</a></h2><ul>
    {foreach item=d from=$c.children name=sitemap} 
<li><a title="{$d.site_name}" href="{$d.site_url}">{$d.site_name}</a></li>
    {/foreach}
{else}  
<li><h2><a title="{$c.site_name}" href="{$c.site_url}">{$c.site_name}</a></h2><ul>
{/if}
</ul>
</li>
{/foreach}  
</ul>
 
     
    