- PHP

Make a list from PHP Array

To make a list from an array in PHP, simply make an HTML list with <ul> and <li> tags while looping through the array.

Example 1:

Make a list from simple PHP array.

<?php 

$array = ['dog', 'cat', 'snake'];

echo '<ul>';

foreach($array as $value) {
  echo '<li>'.$value.'</li>';
}

echo '</ul>';

Output:

  • dog
  • cat
  • snake

Example 2:

Make a list from associative PHP array.

<?php 

$array = [
  "Name" => "Anna",
  "Age" => 37,
];

echo '<ul>';

foreach($array as $key => $value) {
  echo '<li>'.$key . ': ' . $value.'</li>';
}

echo '</ul>';

Output:

  • Name: Anna
  • Age: 37