Explode to array and print each element as list item

Explode to array and print each element as list item

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Explode to Array and Print Each Element as List Item

A common pattern in web development involves retrieving a string of comma-separated values from a database field and converting it into a usable array structure. Once you have that data, the next logical step is often to iterate through it to render HTML elements, such as list items. The code snippet provided demonstrates an attempt to execute this workflow using PHP's explode() function combined with a foreach loop. While the logic for splitting the string is sound, the implementation contains a critical variable scoping error that prevents the script from functioning correctly.

The first step, $array = explode(',', $set_of_numbers);, correctly isolates individual values into an array. However, the issue arises in the second block where the foreach loop attempts to render these items. In the provided code:

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

The developer has confused two distinct variables. The variable $set_of_numbers initially holds the comma-separated string retrieved from the database. It is used as the argument for explode(). Inside the loop, however, the code assigns the current item to $list_item but then reassigns the original string (or an undefined value depending on context) to $set_of_numbers inside the loop declaration or logic flow implicitly by referencing it again.

Specifically, the line foreach ($array as $list_item => $set_of_numbers) is syntactically valid only if $array contains key-value pairs. Since explode() returns a numerically indexed array where keys are integers starting from 0, using an alias like $list_item for the index and reusing $set_of_numbers for the value creates ambiguity. More importantly, the code attempts to call print_r(array_list_items($set_of_numbers)) inside the loop. This function call relies on $set_of_numbers holding a single element from the array, but the variable name is identical to the source string passed to explode().

To fix this, you must strictly separate the container (the full list of numbers) from the current item being processed. The corrected logic should look like this:

// Retrieve the comma-separated string from the database
$set_of_numbers = "123,456,789"; // Example data

// Step 1: Explode the string into an array
$number_array = explode(',', $set_of_numbers);

// Step 2: Iterate over the array using a clean variable name for the current element
foreach ($number_array as $item) {
    echo "<li>" . htmlspecialchars($item) . "</li>";
}

In this corrected version, $number_array holds the complete list, while $item represents the single value during iteration. This eliminates the confusion between the source string and the current element. Additionally, using htmlspecialchars() is a best practice here to prevent XSS vulnerabilities if the database field contains user-generated content, even though simple numbers are unlikely to trigger this in practice.

Another consideration is handling empty strings or trailing commas within the database field. If the input is "123,,456", explode() will create an array containing an empty string between the commas. Depending on your requirements, you might want to filter these out before rendering:

$number_array = array_filter(explode(',', $set_of_numbers));

This ensures that only non-empty values are iterated over, preventing empty list items from appearing in your output. If the array_list_items() function referenced in the original snippet is a custom helper meant to format the number further, ensure it accepts a single string as an argument and not the entire array.

For developers working within the Laravel ecosystem, similar operations are frequently performed when handling bulk data or importing CSV-like fields stored in text columns. The principle remains identical: parse the string once into an array, then iterate cleanly. If you need to handle this logic dynamically across multiple models or views, consider encapsulating the parsing logic in a service class rather than repeating explode calls throughout your codebase.

By distinguishing between the collection and the individual element, and by sanitizing output before rendering HTML tags, you ensure that the list generation is both robust and secure.

Our network