Skip to the content.

PHP Array To Text Table

PHP-class, which allows to transform php associative arrays to cool ASCII tables.

BlueYellow Ukraine ❤

Installation

Simply run composer require:

composer require dekor/php-array-table

or add to composer.json:

"dekor/php-array-table": "^2.0"

or try out using it in a sandbox: https://play.phpsandbox.io/dekor/php-array-table/6WdN1pkx8NK43yOV

Usage

use dekor\ArrayToTextTable;

$data = [
    [
        'id' => 1,
        'name' => 'Denis Koronets',
        'role' => 'php developer',
    ],
    [
        'id' => 2,
        'name' => 'Maxim Ambroskin',
        'role' => 'java developer',
    ],
    [
        'id' => 3,
        'name' => 'Andrew Sikorsky',
        'role' => 'php developer',
    ]
];

echo (new ArrayToTextTable($data))->render();

Will draw the next output:

+----+-----------------+----------------+
| id | name            | role           |
+----+-----------------+----------------+
| 1  | Denis Koronets  | php developer  |
| 2  | Maxim Ambroskin | java developer |
| 3  | Andrew Sikorsky | php developer  |
+----+-----------------+----------------+

Formatters (since v2)

Version 2 introduces a new feature that allows to pre and postprocess column data by applying filters.

You’re able to develop your own formatters by extending BaseColumnFormatter and implementing abstract methods.

List of formatters out of the box:

use dekor\ArrayToTextTable;
use dekor\formatters\AlignFormatter;

$data = [
    [
        'left' => 2,
        'center' => 'Dummy one',
        'right' => 14.33,
    ],
    [
        'left' => 3,
        'center' => 'Another great day for a great inventers!',
        'right' => 1,
    ],
];

$builder = new ArrayToTextTable($data);
$builder->applyFormatter(new AlignFormatter(['center' => 'center', 'right' => 'right']));

echo $builder->render();

outputs:

+------+------------------------------------------+-------+
| left | center                                   | right |
+------+------------------------------------------+-------+
| 2    |                Dummy one                 | 14.33 |
| 3    | Another great day for a great inventers! |     1 |
+------+------------------------------------------+-------+

$data = [ [ ‘left’ => 1, ‘right’ => 2.89, ] ];

$builder = new ArrayToTextTable($data); $builder->applyFormatter(new SprintfFormatter([‘left’ => ‘%01.3f’, ‘right’ => ‘%03.3f’]));

echo $builder->render();


outputs:
<pre>
+-------+-------+
| left  | right |
+-------+-------+
| 1.000 | 2.890 |
+-------+-------+
</pre>

- `ColorFormatter` - allows to highlight text with specific color (only works in terminal):
```php
use dekor\ArrayToTextTable;
use dekor\formatters\ColorFormatter;

$data = [
    ['test' => 1],
    ['test' => -1],
];

$builder = new ArrayToTextTable($data);
$builder->applyFormatter(new ColorFormatter(['test' => fn ($value) => $value > 0 ? 'Red' : 'Green']));

echo $builder->render() . PHP_EOL;

outputs:

img.png

Allowed colors list (see ColorFormatter::$colors)

Our sponsors list: