Explode to array and print each element as list item

user2170133 picture user2170133 · Apr 27, 2013 · Viewed 70.2k times · Source

I have a set of numbers in a table field in database, the numbers are separated by comma ','. I am trying to do the following:

Step 1. : SELECT set of numbers from database and explode it to array :

$array =  explode(',', $set_of_numbers);

Step 2. : Print each element of the array as list item by using foreach loop :

foreach ($array as $list_item => $set_of_numbers){
    echo "<li>";
    print_r(array_list_items($set_of_numbers));
    echo "</li>";}

Please anybody tell me what is wrong. Thank you.

Answer

Danil Speransky picture Danil Speransky · Apr 27, 2013
$numbers = '1,2,3';

$array =  explode(',', $numbers);

foreach ($array as $item) {
    echo "<li>$item</li>";
}