PHP How to Print Array: Practical Examples (print r, var dump, foreach)
Fri Aug 29th, 2025 — 1 day ago

How to Print an Array — PHP Print Array Examples

This guide shows practical ways to use PHP to print an array for debugging and output. We’ll cover several PHP functions that assist in printing and “echoing” content into your website.

Problem

  • You need to print an array in PHP for debugging or display.
  • Directly echoing an array prints “Array” or triggers notices.
  • You need PHP print array examples.

Solutions

PHP: Print an Array Function

Here’s how you can use PHP to print an array, with the print_r() function for quick, readable output:

<?php
$users = ['alice', 'bob', 'carol'];
print_r($users);

The print_r function call in PHP performs a human-readable dump, optionally returning a string:

<?php
$data = ['id' => 10, 'tags' => ['a', 'b']];
print_r($data);                // prints
$txt = print_r($data, true);   // returns string
file_put_contents('dump.txt', $txt);

A print_r array PHP operation often pairs with <pre> for formatting in HTML:

<?php
$cfg = ['host' => 'localhost', 'port' => 5432];
echo '<pre>';
print_r($cfg);
echo '</pre>';

Echo Array Value in PHP

The phrase echo array value PHP means echoing a single element, not the whole array.

<?php
$user = ['name' => 'Ada', 'role' => 'admin'];
echo $user['name'];   // Ada

Echo Array in PHP

This echo array PHP attempt fails because echo expects a string. Use implode() for simple indexed arrays.

<?php
$nums = [10, 20, 30];
echo implode(', ', $nums);  // 10, 20, 30

Bonus: PHP var_dump() For Types

Use PHP’s var_dump() function to see types and lengths:

<?php
$mix = ['n' => 42, 's' => 'hi', 'b' => true];
var_dump($mix);

Bonus: var_export() For Reproducible Code

var_export() outputs valid PHP code that recreates the value:

<?php
$arr = ['x' => 1, 'y' => [2,3]];
echo '<pre>' . var_export($arr, true) . '</pre>';

Bonus: Pretty JSON For APIs

Pretty-print PHP arrays as JSON for logs or API responses:

<?php
$payload = ['ok' => true, 'items' => [1,2,3]];
echo json_encode($payload, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);

Things to Consider

Printing an array in PHP often implies HTML context; wrap dumps in <pre> for readability and escape when showing user data.

  • print_r() is quick; var_dump() shows types; var_export() is reproducible.
  • JSON_PRETTY_PRINT requires PHP 5.4+.

PHP Pretty Print Json

We can perform a PHP pretty print JSON format operation using JSON_PRETTY_PRINT, as an optional flag, passed to json_encode():

<?php
echo json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);

How To Print Multidimensional Array In PHP Using For Loop

Here’s how to print a multi-dimensional array in PHP using a for loop by iterating indices manually, but foreach is simpler:

<?php
$m = [[1,2], [3,4]];
for ($i = 0; $i < count($m); $i++) {
  for ($j = 0; $j < count($m[$i]); $j++) {
    echo $m[$i][$j] . PHP_EOL;
  }
}

Gotchas

  • echo $array prints “Array” or triggers “Array to string conversion”.
  • Never dump sensitive data in production logs.
  • Escape user-supplied values with htmlspecialchars() to avoid XSS in HTML.
  • Large dumps can be slow; limit or sample output.
  • var_dump() output is noisy; prefer print_r() for readability in HTML with <pre>.

Sources


Further Investigation

Printing arrays in PHP may involve formatting helpers or wrappers that toggle between print_r, var_dump, and JSON.

PHP: Print an Array as a Table Example

Here’s how you can use PHP to print an array as a table by iterating and building <table> rows for keys and values:

<?php
$assoc = ['name'=>'Ada','role'=>'admin'];
echo "<table>\n";
foreach ($assoc as $k => $v) {
  echo "<tr><th>" . htmlspecialchars($k) . "</th><td>" . htmlspecialchars((string)$v) . "</td></tr>\n";
}
echo "</table>";

PHP: Print Array Foreach Example

Now let’s do a PHP print array foreach() call that uses key/value pairs for full control:

<?php
$arr = ['a'=>1,'b'=>2];
foreach ($arr as $k => $v) {
  echo "$k => $v" . PHP_EOL;
}

TL;DR

  • Use print_r($arr) for quick, readable output; use var_dump($arr) for types; use implode(', ', $arr) for flat arrays.
<?php
echo '<pre>';
print_r($arr);       // quick
var_dump($arr);      // types
echo implode(', ', [10,20,30]); // flat list
echo '</pre>';