Display array values in a new line on html
You can use a foreach
loop in PHP to iterate over an array and display its values in a new line on an HTML page. Here's an example:
<?php
$array = array("apple", "banana", "cherry", "date", "elderberry");
?>
<ul>
<?php foreach ($array as $value) {?>
<li><?php echo $value;?></li>
<?php }?>
</ul>
This will output:
<ul>
<li>apple</li>
<li>banana</li>
<li>cherry</li>
<li>date</li>
<li>elderberry</li>
</ul>
Alternatively, you can use the implode
function to concatenate the array values with a newline character (\n
) and then echo the result:
<?php
$array = array("apple", "banana", "cherry", "date", "elderberry");
echo implode("\n", $array);
?>
This will output:
apple
banana
cherry
date
elderberry
You can also use JavaScript to achieve this:
<script>
var array = ["apple", "banana", "cherry", "date", "elderberry"];
var html = "";
for (var i = 0; i < array.length; i++) {
html += array[i] + "<br>";
}
document.getElementById("output").innerHTML = html;
</script>
<div id="output"></div>
This will output:
apple
banana
cherry
date
elderberry
Make sure to replace #output
with the actual ID of the HTML element where you want to display the array values.