How to Create HTML List from Array in PHP?
Last Updated :
06 Jul, 2024
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.
Using foreach Loop
The 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() Function
Using 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() Function
The 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>
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