How to Create HTML List from Array in PHP? Last Updated : 06 Jul, 2024 Comments Improve Suggest changes Like Article Like Report Given an array containing some items, the task is to create an HTML list from an array in PHP. An HTML list is a collection of items enclosed within <ul> (unordered list) or <ol> (ordered list) tags. Each item in the list is enclosed within <li> (list item) tags. This article explores different approaches to converting an array into an HTML list.Table of Content Using foreach Loop Using array_map() and implode() FunctionUsing array_reduce() FunctionUsing foreach LoopThe foreach loop is a basic method to iterate over an array and generate the corresponding HTML list items.The createList function takes an array as an argument.It initializes the $html string with the opening <ul> tag.The foreach loop iterates over each element in the array, appending each item as an <li> element to the $html string.The function closes the list with the </ul> tag and returns the resulting HTML string.Example: This example shows the creation of HTML list from an array using foreach loop. PHP <?php function createList($arr) { $html = '<ul>'; foreach ($arr as $item) { $html .= '<li>' . htmlspecialchars($item) . '</li>'; } $html .= '</ul>'; return $html; } // Driver code $arr = ['HTML', 'CSS', 'JavaScript', 'PHP']; echo createList($arr); ?> Output<ul><li>HTML</li><li>CSS</li><li>JavaScript</li><li>PHP</li></ul>Using array_map() and implode() FunctionUsing array_map() and implode() function provides a functional approach to generate the HTML list.The createList function takes an array as an argument.It uses array_map() to apply a callback function to each element of the array, wrapping each item in <li> tags.The resulting array of list items is then joined into a single string using implode.The function wraps the list items in <ul> tags and returns the resulting HTML string.Example: This example shows the creation of HTML list from an array using array_map() and implode() Function. PHP <?php function createList($arr) { $listItems = array_map(function($item) { return '<li>' . htmlspecialchars($item) . '</li>'; }, $arr); return '<ol>' . implode('', $listItems) . '</ol>'; } // Driver code $arr = ['HTML', 'CSS', 'JavaScript', 'PHP']; echo createList($arr); ?> Output<ol><li>HTML</li><li>CSS</li><li>JavaScript</li><li>PHP</li></ol>Using array_reduce() FunctionThe array_reduce() can be used to build the HTML list by accumulating the result in a single string.The createList function takes an array as an argument.It uses array_reduce() function to iterate over the array and accumulate the list items into the $html string.The initial value for $html is set to the opening <ul> tag.The callback function appends each item as an <li> element to the accumulating string.The function closes the list with the </ul> tag and returns the resulting HTML string.Example: This example shows the creation of HTML list from an array using array_reduce() Function. PHP <?php function createList($arr) { $html = array_reduce($arr, function($carry, $item) { return $carry . '<li>' . htmlspecialchars($item) . '</li>'; }, '<ul>'); return $html . '</ul>'; } // Driver code $arr = ['HTML', 'CSS', 'JavaScript', 'PHP']; echo createList($arr); ?> Output<ul><li>HTML</li><li>CSS</li><li>JavaScript</li><li>PHP</li></ul> Comment More infoAdvertise with us Next Article How to Create HTML List from Array in PHP? B blalverma92 Follow Improve Article Tags : PHP Similar Reads How to create a New Line in PHP ? When working with text output in PHP, like creating a webpage, sending an email, or saving to a file, it's important to know how to add a new line. Adding a new line helps break up the text, makes it easier to read, and keeps it looking right whether it's shown in a browser, a terminal, or a file.Ho 2 min read How to embed PHP code in an HTML page ? PHP is the abbreviation of Hypertext Preprocessor and earlier it was abbreviated as Personal Home Page. We can use PHP in HTML code by simply adding a PHP tag without doing any extra work. Example 1: First open the file in any editor and then write HTML code according to requirement. If we have to a 2 min read How to Encode Array in JSON PHP ? Encoding arrays into JSON format is a common task in PHP, especially when building APIs or handling AJAX requests. Below are the approaches to encode arrays into JSON using PHP: Table of Content Using json_encode()Encoding Associative ArraysCustom JSON SerializationUsing json_encode()PHP provides a 2 min read How to Insert a New Element in an Array in PHP ? In PHP, an array is a type of data structure that allows us to store similar types of data under a single variable. The array is helpful to create a list of elements of similar types, which can be accessed using their index or key.We can insert an element or item in an array using the below function 5 min read How to define a list item in HTML5? To define a list in HTML5 we can use the HTML <li> tag. But there are two types of listing in HTML. One is an ordered list and another one is an unordered list. For ordered we can use HTML <ol> tag and for the unordered list, we will use the HTML <ul> tag. Also, we can change the l 2 min read How to create an array with key value pairs in PHP? In PHP, an array with key-value pairs is called an associative array. It maps specific keys to values, allowing you to access elements using custom keys rather than numerical indices. Keys are strings or integers, while values can be of any data type.Here we have some common approaches to create an 2 min read How to Create an HTML Table from an Object Array Using JavaScript ? Tables are a fundamental part of web development, and displaying data in a structured manner is a common requirement. JavaScript provides a powerful way to dynamically generate HTML content, making it easy to create tables from object arrays. Table of Content Using innerHTML propertyUsing appendChil 2 min read How to remove HTML tags from data in PHP ? Removing HTML tags from data in PHP is a crucial step for sanitizing user input or displaying content safely. This process involves using the strip_tags() function to eliminate any HTML or PHP tags from a string, leaving only plain text. It's essential for preventing potential security risks, such a 1 min read How to parse HTML file in PHP ? In this article, we will learn to parse HTML in PHP. What is parsing? Generally parsing is converting one data type to another. It means how we can convert various data types to HTML. For example: Converting string to HTML. Why do we need parsing? To add the dynamic data (HTML content) at a certain 2 min read Creating a Zero-Filled Array in PHP Creating an array filled with zeros can be useful in various scenarios, such as initializing an array for further manipulation or setting up a default state. This article explores multiple approaches to creating a zero-filled array in PHP. A zero-filled array is an array where each element is initia 3 min read Like