What are some alternative methods to using <ul> and [*] tags for displaying nested categories in PHP?

Using nested <ul> and [*] tags can become cumbersome and difficult to manage when displaying nested categories in PHP. An alternative method is to use recursion to loop through the nested categories and display them in a more organized and readable way.

function displayCategories($categories, $parent_id = 0) {
    echo &#039;&lt;ul&gt;&#039;;
    foreach ($categories as $category) {
        if ($category[&#039;parent_id&#039;] == $parent_id) {
            echo &#039;&lt;li&gt;&#039; . $category[&#039;name&#039;] . &#039;&lt;/li&gt;&#039;;
            displayCategories($categories, $category[&#039;id&#039;]);
        }
    }
    echo &#039;&lt;/ul&gt;&#039;;
}

// Example usage
$categories = [
    [&#039;id&#039; =&gt; 1, &#039;name&#039; =&gt; &#039;Category 1&#039;, &#039;parent_id&#039; =&gt; 0],
    [&#039;id&#039; =&gt; 2, &#039;name&#039; =&gt; &#039;Subcategory 1&#039;, &#039;parent_id&#039; =&gt; 1],
    [&#039;id&#039; =&gt; 3, &#039;name&#039; =&gt; &#039;Subcategory 2&#039;, &#039;parent_id&#039; =&gt; 1],
    [&#039;id&#039; =&gt; 4, &#039;name&#039; =&gt; &#039;Category 2&#039;, &#039;parent_id&#039; =&gt; 0],
    [&#039;id&#039; =&gt; 5, &#039;name&#039; =&gt; &#039;Subcategory 3&#039;, &#039;parent_id&#039; =&gt; 4],
];

displayCategories($categories);