Display array values in PHP

Display array values in PHP

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

Stefan Bogdanescu

Founder & Senior Architect

Displaying Array Values in PHP Without Brackets and Commas

When you first encounter arrays in PHP, the default debugging functions like print_r, var_dump, or var_export are your go-to tools. They are excellent for inspecting structure during development, but they render poorly when output to a user-facing view. These functions wrap every value in a bracketed key assignment format, such as [a] => apple. If you simply echo this array in a loop without processing it, you end up with messy HTML that lacks proper separation and styling.

The core challenge is stripping away the [key] => prefix and ensuring values are separated by commas. While print_r outputs to STDOUT, the same underlying logic applies when generating strings for views or API responses. You need to iterate through the data, extract only the value, and concatenate them with a delimiter.

Using implode() for Simple Lists

The most idiomatic way to join array values into a single string is using the implode() function. This built-in PHP function takes a glue string as the first argument and an array as the second. It iterates over the array, stripping keys entirely and returning a single string where elements are separated by the specified delimiter.

$fruits = ['apple', 'banana', 'orange'];

// Creates: apple, banana, orange
echo implode(', ', $fruits);

This approach is efficient because it avoids manual looping. However, be aware that implode() treats the array as a list of values only. If your original array contains key-value pairs like ['a' => 'apple', 'b' => 'banana'], implode() will simply ignore the keys and return the values separated by the delimiter. This is often exactly what you want, but it also means any specific ordering defined by your keys (other than insertion order in PHP 7.3+) might be lost if you rely solely on the array's internal structure without explicit control.

Handling Associative Arrays Explicitly

If you are working with associative arrays where the key holds semantic meaning—such as ['price' => '5', 'name' => 'Apple']—you must decide whether to display all values or only specific ones. The implode() function will dump every value regardless of its key, which might not align with your design.

In cases where you need to filter which values appear, or if you are building a dynamic form or report, a foreach loop offers finer control. You can check the existence of keys or apply conditional logic before adding the value to the output string.

$data = ['price' => '5', 'name' => 'Apple'];

$output = '';
$first = true;

foreach ($data as $value) {
    if (!$first) {
        $output .= ', ';
    }
    $output .= $value;
    $first = false;
}

echo $output;

While slightly more verbose, this pattern allows you to inject HTML tags or conditional styling around specific items before they are joined into the final string. It is also easier to modify if you need to skip certain keys or transform values dynamically within the loop.

Avoiding print_r() in Production Views

A common mistake for developers new to PHP is using print_r($array) directly in a view file or inside an echo statement. While this works for debugging, it outputs the entire data structure including keys and type information (e.g., string(4) "apple"). This makes the output unreadable for end users and can break CSS layouts due to unpredictable whitespace and character counts.

When integrating with frameworks or building reusable components, always extract the raw values first. If you are using Laravel, consider utilizing helper functions or Blade directives that handle data rendering safely. The underlying principle remains the same: separate data from presentation. By explicitly joining values with implode() or looping through them, you ensure that your output is clean, styled correctly, and independent of the internal array key structure.

Our network