PHP Array to Table Converter

Paste a PHP array literal and read it as a table. Once the array is wide, columns are quicker to scan than print_r output in a browser tab. The literal is parsed in this page, so you do not need a local PHP install. Once it renders, Copy for Email pastes the table into Gmail, Outlook or Google Docs with its borders intact, Copy HTML gives bare <table> markup, and Copy Markdown gives a pipe table for a README or a pull request.

PHP Input
Table Preview
Table will appear here...

1 PHP Array to Table example

PHP Array Input

1
2
3
4
5
[
    ['id' => 1, 'name' => 'John Doe', 'role' => 'Admin', 'active' => true],
    ['id' => 2, 'name' => 'Jane Smith', 'role' => 'User', 'active' => false],
    ['id' => 3, 'name' => 'Bob Johnson', 'role' => 'Editor', 'active' => true]
]

2 What it replaces

print_r in a browser tab

A wide associative array is slow to read as indented text. In a table, you compare one field across every row by reading down a column.

A local PHP install

Nothing is executed. The literal is read as data, so you can paste one from a file and inspect it on a machine with no PHP.

How to use the PHP Array to Table tool

  1. Paste a PHP array literal into the left editor, using either array( ... ) or the short [ ... ] syntax. Upload PHP loads a file and Load URL fetches one; Dummy Data shows a sample.
  2. The table appears as you type. A list of associative arrays becomes rows and columns; a list of plain arrays uses numeric indexes 0, 1, 2 as column headers.
  3. Nested arrays inside a cell are shown as JSON text so the table stays rectangular. Comments starting with // or /* are ignored.
  4. A syntax problem such as a missing comma shows "Error: Expected , or ]" above the editor. Fix the line and the table returns.
  5. Use Copy for Email to paste the table into Gmail, Outlook or Google Docs, Copy HTML for bare <table> markup, or Copy Markdown for a pipe table in a README or pull request.
  6. A record count appears in the footer; Clear empties the editor.

How to display a PHP array as an HTML table

PHP: array to table

<?php
$users = [
    ["id" => 1, "name" => "John Doe", "role" => "Admin", "active" => true],
    ["id" => 2, "name" => "Jane Smith", "role" => "User", "active" => false],
];

// Header from the keys of the first row
$columns = array_keys($users[0]);

echo "<table>\n<thead><tr>";
foreach ($columns as $c) echo "<th>" . htmlspecialchars($c) . "</th>";
echo "</tr></thead>\n<tbody>\n";
foreach ($users as $row) {
    echo "<tr>";
    foreach ($columns as $c) {
        $v = $row[$c] ?? "";
        if (is_bool($v)) $v = $v ? "true" : "false";
        if (is_array($v)) $v = json_encode($v);
        echo "<td>" . htmlspecialchars((string) $v) . "</td>";
    }
    echo "</tr>\n";
}
echo "</tbody>\n</table>\n";

Cast booleans yourself: (string) false is an empty string, so it would render as a blank cell.

PHP: export the array for other tools

<?php
$users = [
    ["id" => 1, "name" => "John Doe", "role" => "Admin"],
    ["id" => 2, "name" => "Jane Smith", "role" => "User"],
];

// var_export() prints valid PHP you can paste into the tool later
var_export($users);
echo PHP_EOL;

// json_encode() hands the same data to JavaScript or Python
echo json_encode($users, JSON_PRETTY_PRINT), PHP_EOL;

// print_r() is for humans only: it is not parseable

JavaScript (browser): render the exported JSON

// The array exported from PHP with json_encode($users)
const users = [
  { id: 1, name: "John Doe", role: "Admin" },
  { id: 2, name: "Jane Smith", role: "User" },
];

const columns = Object.keys(users[0]);
const esc = (v) => String(v ?? "").replace(/&/g, "&amp;").replace(/</g, "&lt;");

const table = document.createElement("table");
table.innerHTML =
  `<thead><tr>${columns.map((c) => `<th>${esc(c)}</th>`).join("")}</tr></thead>` +
  `<tbody>${users.map((u) => `<tr>${columns.map((c) => `<td>${esc(u[c])}</td>`).join("")}</tr>`).join("")}</tbody>`;
document.body.appendChild(table);

Embed the JSON in a page with <?= json_encode($users) ?> inside a script tag, or fetch it from an endpoint.

Common errors and how to fix them

Error: Expected , or ]
A comma is missing between two elements, or a bracket does not match its opener. Check the line before the one that looks wrong; a trailing comma after the last element is fine.
Error: Unterminated string
A quote was opened and never closed, often an apostrophe inside a single-quoted string such as 'John's'. Escape it as \' or switch to double quotes.
print_r() output produces no table
print_r() prints Array ( [id] => 1 ) which is not PHP syntax. Use var_export($arr) instead, which prints a literal the tool can parse.
Column headers are 0, 1, 2 instead of field names
The rows are plain lists rather than associative arrays. Use string keys such as "name" => "John" in each row, or accept the numeric headers.
Variables, constants or function calls show up as raw text
The parser reads literals only. $userId, PHP_EOL or date("Y") cannot be evaluated in the browser. Replace them with the values you expect, or run var_export() on the real array first.

Good to know

The parser reads what var_export() emits: string and numeric keys, integers, floats, booleans, null and nested arrays in either syntax. It does not run PHP, so expressions, heredocs, objects and class constants are not supported. When in doubt, run var_export() on the array on the server and paste the output.

The shape that renders well here, a sequential list of associative arrays with matching keys, is also the shape json_encode() turns into a clean JSON array of objects. Keep your PHP data in that form and it moves between the JSON, CSV and table tools without any restructuring.

Specifications and references

This tool follows the specifications below. Read them when you need the exact normative wording, for example to settle an edge case between two implementations.

Frequently Asked Questions

Which PHP syntaxes are supported?

Both array( ... ) and [ ... ], with string or integer keys and nested arrays.

Can I paste var_export() output?

Yes. var_export() produces valid PHP syntax. print_r() output is not PHP syntax, so it cannot be parsed; use var_export() instead.

What array shape renders best?

An array of associative arrays with matching keys, which maps directly to rows and columns.

How are nested arrays shown?

Nested arrays are shown as JSON text inside the cell, so the table keeps one column per key.

Can I export the table?

For a file, use the PHP Array to CSV or PHP Array to JSON tools with the same input. For the clipboard, use the three copy buttons above the table: Copy for Email, Copy HTML and Copy Markdown.

How do I send a PHP array as a table in an email?

Paste the array, click Copy for Email, then paste into the message in Gmail, Outlook or Apple Mail. The clipboard holds a rich-text table with inline borders and a shaded header, which survives in email clients that strip stylesheets. It also pastes as a native table into Google Docs, Word and Notion, which is handy for sharing a config array or a query result with someone who does not read PHP.

When would I use Copy HTML or Copy Markdown instead?

Copy HTML gives plain <table> markup without styles or classes, for a documentation page, a CMS or an HTML email template that has its own CSS. Copy Markdown gives a GitHub pipe table, for a README, a code review comment or a wiki page, where a rendered table is clearer than a pasted array literal. Both read the table as shown, with nested arrays as JSON text.

Once you can read it

Export it with PHP array to JSON or PHP array to CSV. JSON data has its own viewer at JSON to Table.

More Data Viewers

Browse all data viewers

Copied to clipboard!