How can I echo or print an array in PHP?

How can I echo or print an array in PHP?

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

Stefan Bogdanescu

Founder & Senior Architect

Flattening Output: How to Echo an Array in PHP Without Its Structure

When you dump a multidimensional array to your browser, the default behavior is often counterproductive. The var_dump() or print_r() functions reveal the internal keys ([0], [1]) and nesting levels, which clutter the screen when all you need is the raw data content. In your specific case, you have a nested structure where $results contains an outer key data, which holds an indexed array of objects or associative arrays containing page_id and type.

Your current approach using a foreach loop correctly iterates through the items to extract the type property. However, relying solely on echo $result->type . "\n" produces output separated by newlines but retains the implicit order without context if you ever need to process the whole list as a single string. To truly "echo the content without this structure," you are likely aiming for one of two outcomes: a single concatenated string or a formatted text block where the array syntax is completely stripped away.

The most direct way to strip the array structure while preserving data order is to join the values. If your goal is simply to display the types listed in a continuous stream, implode() is the standard utility. You can map over your collection to extract just the desired fields and then glue them together.

$types = [];
foreach ($results['data'] as $result) {
    $types[] = $result['type'];
}

echo implode(', ', $types);

This approach creates a clean, comma-separated string: WEBSITE, COMMUNITY. It completely removes the visual noise of the array brackets and keys. This is often preferable for logs or simple debug messages where readability matters more than structural fidelity.

However, if your input data consists of objects (indicated by the -> operator in your snippet) rather than associative arrays, you must ensure you are accessing the properties correctly. Your snippet $results['data'] suggests an indexed array inside a named key. If $results is actually a collection of objects where each has a type property directly accessible, the syntax shifts slightly.

Consider this scenario where you want to flatten a nested object structure into a single line:

$data = [];
foreach ($results['data'] as $item) {
    if (isset($item->type)) {
        $data .= $item->type . ', ';
    }
}

echo rtrim($data, ', ');

The rtrim() function here is a crucial detail. Without it, your output will end with an unwanted trailing comma and space. This small oversight is a common pitfall when concatenating strings in loops; the logic works, but the formatting degrades at the very end of the stream.

There is also a functional programming alternative using array_map and implode, which reduces the lines of code significantly. If you are working with an indexed array of associative arrays (like your sample output), you can skip the explicit loop:

echo implode(', ', array_map(fn($r) => $r['type'], $results['data']));

This one-liner is concise and readable for most experienced developers. It maps the type key from every element in $results['data'] and immediately joins them with commas. This method assumes that every element in the array has a type key; if there might be nulls or missing keys, you would need to add a fallback like $r['type'] ?? 'Unknown'.

If your requirement is to remove the outer wrapper entirely and just output the raw values as they exist in memory without any formatting, PHP's json_encode() with specific flags can help. While json_encode usually returns a JSON string (with brackets), you can use JSON_PRETTY_PRINT for readability or simply parse it if you need to manipulate the text further. However, for simple echo purposes, manual joining via implode is generally faster and lighter than invoking JSON serialization just to remove brackets.

Ultimately, the choice depends on whether you want a list of values separated by delimiters or a single unbroken string. If you are building a custom debug tool in Laravel and need to log this specific structure without the clutter, wrapping the logic in a helper method keeps your controllers clean. For complex nested structures where simple flattening isn't enough, consider using a dedicated logging library that respects context, but for quick debugging, implode with array_map remains the most robust native solution.

Our network