How can I print all the values of an array?

How can I print all the values of an array?

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

Stefan Bogdanescu

Founder & Senior Architect

Why print_r Fails and What to Use Instead

When you try to dump an array in a Laravel application expecting raw text, print_r() often falls short. It outputs the structure with type hints and formatting that can clutter your logs or make debugging difficult if you simply want to inspect the contents. If you are asking how to print all the values of an array—whether it is a simple list like ['pie', 'apple', 'orange'] or a complex nested structure—you need to understand the difference between structural inspection and value extraction.

The core issue with print_r() is that it prioritizes showing you what the data is, not just the data. It prints keys and values in pairs and includes type information (like "Array" or "string"). If your goal is to see a clean list of values without the surrounding noise, print_r is the wrong tool for the job.

To extract only the raw values from an associative or indexed array, you must use the array_values() function. This utility strips the keys and reindexes the array numerically starting from zero. Once the keys are gone, you can pass the result to implode() to join them into a single string separated by newlines or spaces.

Here is a practical example of how to achieve this in PHP:

$data = ['pie', 'cake', 'donut'];

// Method 1: Using array_values and implode
$output = implode("\n", array_values($data));
echo $output;

This approach works regardless of whether the original array was indexed or associative. If you have an array like ['name' => 'John', 'age' => 30], print_r() will show Array ( [name] => John [age] => 30 ). Using array_values() first converts that to Array ( [0] => John [1] => 30 ), effectively discarding the semantic keys.

However, there is a significant trade-off here. By using array_values(), you lose access to the original keys. If your downstream logic depends on those specific identifiers, stripping them before printing might not be the right path. In many debugging scenarios within Laravel, developers prefer using json_encode() instead. This function serializes the array into a JSON string, preserving both keys and values in a readable format without the clutter of PHP's internal type representation.

echo json_encode($data, JSON_PRETTY_PRINT);

The JSON_PRETTY_PRINT flag ensures the output is formatted with indentation and newlines, making it much easier to read in browser logs or terminal outputs compared to a single line of minified JSON. This is often safer than trying to strip keys because you retain the full context of the data structure.

If you are working within a Laravel project specifically, consider where this output is going. If you are logging errors using Log::error(), injecting raw text from print_r() can sometimes cause parsing issues depending on the log driver. Using json_encode() ensures consistent formatting across different log systems. For more complex debugging needs, tools like dd() (short for dump()) or dump() allow you to pause execution and inspect variables interactively without cluttering the output stream permanently.

Ultimately, the choice between print_r, array_values with implode, and json_encode depends on whether you need raw values or structural integrity. If you just want to see "pie" followed by other items in a list, stripping keys is valid. If you need to verify data integrity or debug associations, preserving the key-value pairs via JSON serialization is the robust engineering choice.

Our network