{"id":876,"date":"2024-08-20T05:55:12","date_gmt":"2024-08-20T05:55:12","guid":{"rendered":"https:\/\/www.wpdebugfix.com\/?p=876"},"modified":"2025-01-06T09:08:42","modified_gmt":"2025-01-06T09:08:42","slug":"convert-nested-array-to-html-list","status":"publish","type":"post","link":"https:\/\/www.wpdebugfix.com\/convert-nested-array-to-html-list\/","title":{"rendered":"Convert nested array to html (ul li)"},"content":{"rendered":"<h4>Problem: How to Generate an HTML Unordered List from a Nested Array in PHP?<\/h4>\n<p>I have a nested array in PHP that represents a hierarchy with parent-child relationships. I want to generate an HTML unordered list (<code>&lt;ul&gt;<\/code>) from this array, where each level is correctly nested. How can I achieve this?<\/p>\n<p>Here\u2019s an example of the nested array:<\/p>\n<div class=\"code-embed-wrapper\"> <pre class=\"language-php code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-php code-embed-code\">$nestedArray = [<br\/>    [<br\/>        &#039;id&#039; =&gt; 1,<br\/>        &#039;name&#039; =&gt; &#039;Parent 1&#039;,<br\/>        &#039;children&#039; =&gt; [<br\/>            [<br\/>                &#039;id&#039; =&gt; 2,<br\/>                &#039;name&#039; =&gt; &#039;Child 1.1&#039;,<br\/>                &#039;children&#039; =&gt; [<br\/>                    [<br\/>                        &#039;id&#039; =&gt; 4,<br\/>                        &#039;name&#039; =&gt; &#039;Child 1.1.1&#039;<br\/>                    ]<br\/>                ]<br\/>            ],<br\/>            [<br\/>                &#039;id&#039; =&gt; 3,<br\/>                &#039;name&#039; =&gt; &#039;Child 1.2&#039;<br\/>            ]<br\/>        ]<br\/>    ],<br\/>    [<br\/>        &#039;id&#039; =&gt; 5,<br\/>        &#039;name&#039; =&gt; &#039;Parent 2&#039;,<br\/>        &#039;children&#039; =&gt; [<br\/>            [<br\/>                &#039;id&#039; =&gt; 6,<br\/>                &#039;name&#039; =&gt; &#039;Child 2.1&#039;<br\/>            ]<br\/>        ]<br\/>    ]<br\/>];<\/code><\/pre> <div class=\"code-embed-infos\"> <\/div> <\/div>\n<p><strong>I want to transform it into an HTML structure like this:<\/strong><\/p>\n<div class=\"code-embed-wrapper\"> <pre class=\"language-php code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-php code-embed-code\">&lt;ul&gt;<br\/>    &lt;li&gt;Parent 1<br\/>        &lt;ul&gt;<br\/>            &lt;li&gt;Child 1.1<br\/>                &lt;ul&gt;<br\/>                    &lt;li&gt;Child 1.1.1&lt;\/li&gt;<br\/>                &lt;\/ul&gt;<br\/>            &lt;\/li&gt;<br\/>            &lt;li&gt;Child 1.2&lt;\/li&gt;<br\/>        &lt;\/ul&gt;<br\/>    &lt;\/li&gt;<br\/>    &lt;li&gt;Parent 2<br\/>        &lt;ul&gt;<br\/>            &lt;li&gt;Child 2.1&lt;\/li&gt;<br\/>        &lt;\/ul&gt;<br\/>    &lt;\/li&gt;<br\/>&lt;\/ul&gt;<\/code><\/pre> <div class=\"code-embed-infos\"> <\/div> <\/div>\n<p>How can I create a recursive function in PHP to generate this HTML?<\/p>\n<h3><\/h3>\n<h3><strong>Solution: Generating an HTML Unordered List from a Nested Array Using PHP<\/strong><\/h3>\n<p>To create a nested unordered list from a nested array, you need a recursive function that loops through each item, checks if it has children, and outputs the appropriate HTML structure.<\/p>\n<h4><strong>Step-by-Step Implementation:<\/strong><\/h4>\n<ol>\n<li>We create a recursive function that builds the HTML list based on the given nested array.<\/li>\n<li>The function checks if each item has children, and if it does, it generates another nested list.<\/li>\n<\/ol>\n<p>Here\u2019s how to do it:<\/p>\n<div class=\"code-embed-wrapper\"> <pre class=\"language-php code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-php code-embed-code\">function generateNestedList(array $items) {<br\/>    $html = &#039;&lt;ul&gt;&#039;;<br\/><br\/>    foreach ($items as $item) {<br\/>        $html .= &#039;&lt;li&gt;&#039; . htmlspecialchars($item[&#039;name&#039;]);<br\/><br\/>        if (!empty($item[&#039;children&#039;])) {<br\/>            $html .= generateNestedList($item[&#039;children&#039;]);<br\/>        }<br\/><br\/>        $html .= &#039;&lt;\/li&gt;&#039;;<br\/>    }<br\/><br\/>    $html .= &#039;&lt;\/ul&gt;&#039;;<br\/><br\/>    return $html;<br\/>}<br\/><br\/>\/\/ Example nested array<br\/>$nestedArray = [<br\/>    [<br\/>        &#039;id&#039; =&gt; 1,<br\/>        &#039;name&#039; =&gt; &#039;Parent 1&#039;,<br\/>        &#039;children&#039; =&gt; [<br\/>            [<br\/>                &#039;id&#039; =&gt; 2,<br\/>                &#039;name&#039; =&gt; &#039;Child 1.1&#039;,<br\/>                &#039;children&#039; =&gt; [<br\/>                    [<br\/>                        &#039;id&#039; =&gt; 4,<br\/>                        &#039;name&#039; =&gt; &#039;Child 1.1.1&#039;<br\/>                    ]<br\/>                ]<br\/>            ],<br\/>            [<br\/>                &#039;id&#039; =&gt; 3,<br\/>                &#039;name&#039; =&gt; &#039;Child 1.2&#039;<br\/>            ]<br\/>        ]<br\/>    ],<br\/>    [<br\/>        &#039;id&#039; =&gt; 5,<br\/>        &#039;name&#039; =&gt; &#039;Parent 2&#039;,<br\/>        &#039;children&#039; =&gt; [<br\/>            [<br\/>                &#039;id&#039; =&gt; 6,<br\/>                &#039;name&#039; =&gt; &#039;Child 2.1&#039;<br\/>            ]<br\/>        ]<br\/>    ]<br\/>];<br\/><br\/>\/\/ Generate the HTML unordered list<br\/>echo generateNestedList($nestedArray);<\/code><\/pre> <div class=\"code-embed-infos\"> <\/div> <\/div>\n<p>&nbsp;<\/p>\n<h4><strong>Explanation:<\/strong><\/h4>\n<ul>\n<li>The <code>generateNestedList()<\/code> function takes the nested array as its input.<\/li>\n<li>It starts by creating an opening <code>&lt;ul&gt;<\/code> tag.<\/li>\n<li>The function then loops through each item in the array and creates an <code>&lt;li&gt;<\/code> element for each item\u2019s name.<\/li>\n<li>If the item has children (i.e., a <code>children<\/code> key that is not empty), the function calls itself recursively to generate the nested list.<\/li>\n<li>The function finally closes the <code>&lt;ul&gt;<\/code> tag and returns the complete HTML structure.<\/li>\n<\/ul>\n<h4><strong>Output:<\/strong><\/h4>\n<p>The function generates the following HTML:<\/p>\n<div class=\"code-embed-wrapper\"> <pre class=\"language-php code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-php code-embed-code\">&lt;ul&gt;<br\/>    &lt;li&gt;Parent 1<br\/>        &lt;ul&gt;<br\/>            &lt;li&gt;Child 1.1<br\/>                &lt;ul&gt;<br\/>                    &lt;li&gt;Child 1.1.1&lt;\/li&gt;<br\/>                &lt;\/ul&gt;<br\/>            &lt;\/li&gt;<br\/>            &lt;li&gt;Child 1.2&lt;\/li&gt;<br\/>        &lt;\/ul&gt;<br\/>    &lt;\/li&gt;<br\/>    &lt;li&gt;Parent 2<br\/>        &lt;ul&gt;<br\/>            &lt;li&gt;Child 2.1&lt;\/li&gt;<br\/>        &lt;\/ul&gt;<br\/>    &lt;\/li&gt;<br\/>&lt;\/ul&gt;<\/code><\/pre> <div class=\"code-embed-infos\"> <\/div> <\/div>\n<p>&nbsp;<\/p>\n<h3><strong>Conclusion:<\/strong><\/h3>\n<p>This solution demonstrates how to convert a nested array into an HTML unordered list using a recursive function in PHP. This approach is ideal for dynamically generating hierarchical data, such as categories, menus, or organizational structures.<\/p>\n<p>By utilizing recursion, you can easily handle deeply nested arrays and create structured HTML output.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem: How to Generate an HTML Unordered List from a Nested Array in PHP? I have a nested array in PHP that represents a hierarchy with parent-child relationships. I want to generate an HTML unordered list (&lt;ul&gt;) from this array, where each level is correctly nested. How can I achieve this? Here\u2019s an example of<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1,42,4,5],"tags":[],"class_list":["post-876","post","type-post","status-publish","format-standard","hentry","category-html","category-php","category-web-development","category-website"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.7 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Convert nested array to html (ul li) - WP DEBUG FIX<\/title>\n<meta name=\"description\" content=\"Learn how to create an HTML unordered list from a nested array in PHP using a recursive function. Perfect for hierarchical data structures.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wpdebugfix.com\/convert-nested-array-to-html-list\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Convert nested array to html (ul li) - WP DEBUG FIX\" \/>\n<meta property=\"og:description\" content=\"Learn how to create an HTML unordered list from a nested array in PHP using a recursive function. Perfect for hierarchical data structures.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wpdebugfix.com\/convert-nested-array-to-html-list\/\" \/>\n<meta property=\"og:site_name\" content=\"WP DEBUG FIX\" \/>\n<meta property=\"article:published_time\" content=\"2024-08-20T05:55:12+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-01-06T09:08:42+00:00\" \/>\n<meta name=\"author\" content=\"Neeraj Chaturvedi\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Neeraj Chaturvedi\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.wpdebugfix.com\/convert-nested-array-to-html-list\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.wpdebugfix.com\/convert-nested-array-to-html-list\/\"},\"author\":{\"name\":\"Neeraj Chaturvedi\",\"@id\":\"https:\/\/www.wpdebugfix.com\/#\/schema\/person\/b39b3a28a8489888c28dbee4afdff3bb\"},\"headline\":\"Convert nested array to html (ul li)\",\"datePublished\":\"2024-08-20T05:55:12+00:00\",\"dateModified\":\"2025-01-06T09:08:42+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.wpdebugfix.com\/convert-nested-array-to-html-list\/\"},\"wordCount\":800,\"publisher\":{\"@id\":\"https:\/\/www.wpdebugfix.com\/#organization\"},\"articleSection\":[\"HTML\",\"PHP\",\"Web Development\",\"Website\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wpdebugfix.com\/convert-nested-array-to-html-list\/\",\"url\":\"https:\/\/www.wpdebugfix.com\/convert-nested-array-to-html-list\/\",\"name\":\"Convert nested array to html (ul li) - WP DEBUG FIX\",\"isPartOf\":{\"@id\":\"https:\/\/www.wpdebugfix.com\/#website\"},\"datePublished\":\"2024-08-20T05:55:12+00:00\",\"dateModified\":\"2025-01-06T09:08:42+00:00\",\"description\":\"Learn how to create an HTML unordered list from a nested array in PHP using a recursive function. Perfect for hierarchical data structures.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.wpdebugfix.com\/convert-nested-array-to-html-list\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wpdebugfix.com\/convert-nested-array-to-html-list\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wpdebugfix.com\/convert-nested-array-to-html-list\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.wpdebugfix.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Convert nested array to html (ul li)\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wpdebugfix.com\/#website\",\"url\":\"https:\/\/www.wpdebugfix.com\/\",\"name\":\"WP DEBUG FIX\",\"description\":\"Dedicated support\",\"publisher\":{\"@id\":\"https:\/\/www.wpdebugfix.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wpdebugfix.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.wpdebugfix.com\/#organization\",\"name\":\"WP DEBUG FIX\",\"url\":\"https:\/\/www.wpdebugfix.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.wpdebugfix.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.wpdebugfix.com\/wp-content\/uploads\/2021\/05\/LOGO-WP-DEBUG.png\",\"contentUrl\":\"https:\/\/www.wpdebugfix.com\/wp-content\/uploads\/2021\/05\/LOGO-WP-DEBUG.png\",\"width\":1075,\"height\":168,\"caption\":\"WP DEBUG FIX\"},\"image\":{\"@id\":\"https:\/\/www.wpdebugfix.com\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wpdebugfix.com\/#\/schema\/person\/b39b3a28a8489888c28dbee4afdff3bb\",\"name\":\"Neeraj Chaturvedi\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.wpdebugfix.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/abf47d2fc790848d97f730be41061900a9f8f7aeba298a1597537438b0f0be9f?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/abf47d2fc790848d97f730be41061900a9f8f7aeba298a1597537438b0f0be9f?s=96&d=mm&r=g\",\"caption\":\"Neeraj Chaturvedi\"},\"sameAs\":[\"https:\/\/www.wpdebugfix.com\"],\"url\":\"https:\/\/www.wpdebugfix.com\/author\/admin\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Convert nested array to html (ul li) - WP DEBUG FIX","description":"Learn how to create an HTML unordered list from a nested array in PHP using a recursive function. Perfect for hierarchical data structures.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wpdebugfix.com\/convert-nested-array-to-html-list\/","og_locale":"en_US","og_type":"article","og_title":"Convert nested array to html (ul li) - WP DEBUG FIX","og_description":"Learn how to create an HTML unordered list from a nested array in PHP using a recursive function. Perfect for hierarchical data structures.","og_url":"https:\/\/www.wpdebugfix.com\/convert-nested-array-to-html-list\/","og_site_name":"WP DEBUG FIX","article_published_time":"2024-08-20T05:55:12+00:00","article_modified_time":"2025-01-06T09:08:42+00:00","author":"Neeraj Chaturvedi","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Neeraj Chaturvedi","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.wpdebugfix.com\/convert-nested-array-to-html-list\/#article","isPartOf":{"@id":"https:\/\/www.wpdebugfix.com\/convert-nested-array-to-html-list\/"},"author":{"name":"Neeraj Chaturvedi","@id":"https:\/\/www.wpdebugfix.com\/#\/schema\/person\/b39b3a28a8489888c28dbee4afdff3bb"},"headline":"Convert nested array to html (ul li)","datePublished":"2024-08-20T05:55:12+00:00","dateModified":"2025-01-06T09:08:42+00:00","mainEntityOfPage":{"@id":"https:\/\/www.wpdebugfix.com\/convert-nested-array-to-html-list\/"},"wordCount":800,"publisher":{"@id":"https:\/\/www.wpdebugfix.com\/#organization"},"articleSection":["HTML","PHP","Web Development","Website"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.wpdebugfix.com\/convert-nested-array-to-html-list\/","url":"https:\/\/www.wpdebugfix.com\/convert-nested-array-to-html-list\/","name":"Convert nested array to html (ul li) - WP DEBUG FIX","isPartOf":{"@id":"https:\/\/www.wpdebugfix.com\/#website"},"datePublished":"2024-08-20T05:55:12+00:00","dateModified":"2025-01-06T09:08:42+00:00","description":"Learn how to create an HTML unordered list from a nested array in PHP using a recursive function. Perfect for hierarchical data structures.","breadcrumb":{"@id":"https:\/\/www.wpdebugfix.com\/convert-nested-array-to-html-list\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wpdebugfix.com\/convert-nested-array-to-html-list\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wpdebugfix.com\/convert-nested-array-to-html-list\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.wpdebugfix.com\/"},{"@type":"ListItem","position":2,"name":"Convert nested array to html (ul li)"}]},{"@type":"WebSite","@id":"https:\/\/www.wpdebugfix.com\/#website","url":"https:\/\/www.wpdebugfix.com\/","name":"WP DEBUG FIX","description":"Dedicated support","publisher":{"@id":"https:\/\/www.wpdebugfix.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wpdebugfix.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.wpdebugfix.com\/#organization","name":"WP DEBUG FIX","url":"https:\/\/www.wpdebugfix.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.wpdebugfix.com\/#\/schema\/logo\/image\/","url":"https:\/\/www.wpdebugfix.com\/wp-content\/uploads\/2021\/05\/LOGO-WP-DEBUG.png","contentUrl":"https:\/\/www.wpdebugfix.com\/wp-content\/uploads\/2021\/05\/LOGO-WP-DEBUG.png","width":1075,"height":168,"caption":"WP DEBUG FIX"},"image":{"@id":"https:\/\/www.wpdebugfix.com\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/www.wpdebugfix.com\/#\/schema\/person\/b39b3a28a8489888c28dbee4afdff3bb","name":"Neeraj Chaturvedi","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.wpdebugfix.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/abf47d2fc790848d97f730be41061900a9f8f7aeba298a1597537438b0f0be9f?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/abf47d2fc790848d97f730be41061900a9f8f7aeba298a1597537438b0f0be9f?s=96&d=mm&r=g","caption":"Neeraj Chaturvedi"},"sameAs":["https:\/\/www.wpdebugfix.com"],"url":"https:\/\/www.wpdebugfix.com\/author\/admin\/"}]}},"_links":{"self":[{"href":"https:\/\/www.wpdebugfix.com\/wp-json\/wp\/v2\/posts\/876","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wpdebugfix.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wpdebugfix.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wpdebugfix.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wpdebugfix.com\/wp-json\/wp\/v2\/comments?post=876"}],"version-history":[{"count":1,"href":"https:\/\/www.wpdebugfix.com\/wp-json\/wp\/v2\/posts\/876\/revisions"}],"predecessor-version":[{"id":877,"href":"https:\/\/www.wpdebugfix.com\/wp-json\/wp\/v2\/posts\/876\/revisions\/877"}],"wp:attachment":[{"href":"https:\/\/www.wpdebugfix.com\/wp-json\/wp\/v2\/media?parent=876"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wpdebugfix.com\/wp-json\/wp\/v2\/categories?post=876"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wpdebugfix.com\/wp-json\/wp\/v2\/tags?post=876"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}