DEV Community

Samuel Akopyan
Samuel Akopyan

Posted on

Introducing PrettyPrint — a PHP array pretty-printer with Python-/PyTorch-style formatting

I'm excited to announce PrettyPrint, a small, zero-dependency PHP utility designed to format numeric arrays in a clean, readable style inspired by Python and the tensor-views you’ll see in PyTorch.

Whether you're doing ML experiments, debugging data pipelines, logging arrays, or building educational tools, PrettyPrint makes it easier to inspect array data in a structured way.

Why use PrettyPrint?

No extra dependencies - just pure PHP.

Supports aligned 2D tables, summarized tensor-style views (for larger arrays), 3D tensor with head/tail blocks, and flexible output options (labels, controlling newline behaviour, etc.).

Makes your array dumps more readable and visually helpful.

Quick Start & Examples

Installation

composer require apphp/pretty-print
Enter fullscreen mode Exit fullscreen mode

Global helper functions

You can use the pprint() helper function for quick prints:

Print scalars/strings

pprint('Hello', 123, 4.56);
// Output:
// Hello 123 4.5600
Enter fullscreen mode Exit fullscreen mode

1D / 2D examples

// Print multiple 1D rows aligned as a 2D table
pprint([1, 23, 456], [12, 3, 45]);
// Output:
// [[  1,  23, 456],
//  [12,   3,  45]]
Enter fullscreen mode Exit fullscreen mode

Label + 2D matrix

pprint('Confusion matrix:', [[1,23], [456,7]]);
// Output:
// Confusion matrix:
// [[   1,  23],
//  [ 456,   7]]
Enter fullscreen mode Exit fullscreen mode

Tensor / summarization example

$matrix = [
    [1, 2,  3,  4,  5],
    [6, 7,  8,  9, 10],
    [11,12, 13, 14, 15],
];
pprint($matrix);
// Output:
// tensor([
//   [  1,   2,   3,   4,   5],
//   [  6,   7,   8,   9,  10],
//   [ 11,  12,  13,  14,  15]
// ])
Enter fullscreen mode Exit fullscreen mode

And for summarization (showing just head + tail of rows/cols):

pprint($matrix, headRows:1, tailRows:1, headCols:2, tailCols:2);
// Output:
// tensor([
//   [  1,   2,  ...,   4,   5],
//   ...,
//   [ 11,  12,  ...,  14,  15]
// ])
Enter fullscreen mode Exit fullscreen mode

You can even print 3D tensor-like structures with summarised blocks:

$tensor3d = [
    [[1,2,3],[4,5,6]],
    [[7,8,9],[10,11,12]],
    [[13,14,15],[16,17,18]],
];
pprint($tensor3d, headB:1, tailB:1, headRows:1, tailRows:1, headCols:1, tailCols:1);
// Output:
// tensor([
//  [[ 1,  ...,  3],
//   [ 4,  ...,  6]],
//
//  ...,
//
//  [[13,  ..., 15],
//   [16,  ..., 18]]
// ])
Enter fullscreen mode Exit fullscreen mode

Object-style usage

If you prefer, you can instantiate the printer as an object:

use Apphp\PrettyPrint\PrettyPrint;

$pp = new PrettyPrint();
$pp('Hello', 42);       // same as pprint('Hello', 42)

$pp($tensor3d, headB:2, tailB:1, headRows:1, tailRows:1, headCols:1, tailCols:1);
$pp('Metrics:', [[0.91, 0.02], [0.03, 0.88]]);
Enter fullscreen mode Exit fullscreen mode

Options reference

Here are a few of the core options you can pass:

end (string) — line terminator (default: newline)
headB / tailB (ints) — number of 2D blocks shown for 3D tensors
headRows / tailRows — number of head/tail rows per slice with ellipsis between
headCols / tailCols — number of head/tail columns per slice with ellipsis between

Named-argument syntax (PHP 8+) is supported, or you can pass an array of options as last parameter.
GitHub

Where it shines

ML/AI workflows: When you have numeric arrays or tensors in PHP (or imported from elsewhere) and want human-readable dumps.

Debugging & logging: Replace ad-hoc var_dump() or print_r() with structured, aligned table views.

Educational/teaching code: When showing matrix/tensor examples and you want a cleaner presentation.

CLI & scripts: For quick visual inspection of matrices/tensors in terminal output.

Final thoughts

I believe PrettyPrint fills a small but useful niche for PHP developers working with numeric data or coming from Python/PyTorch mindsets - offering readability, alignment, summarization, and minimal overhead.

Give it a spin:

I'd love your feedback, suggestions for enhancements, or contributions — open an issue or PR and let’s make this tool even better together!

Happy coding!

Top comments (0)