PHP library to display arrays as text
Switch branches/tags
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
src Add __toString method Aug 21, 2015
.gitignore Initial commit Jul 6, 2015
LICENSE Initial commit Jul 6, 2015
README.md Update README.md Aug 21, 2015
composer.json Initial commit Jul 6, 2015
composer.lock Update composer.lock Aug 21, 2015

README.md

ArrayToTextTable

Installation

composer require mathieuviossat/arraytotexttable
{
    "require": {
        "mathieuviossat/arraytotexttable": "~1.0.0"
    }
}

Example

use MathieuViossat\Util\ArrayToTextTable;

$data = [
    [
        'firstname' => 'Mollie',
        'surname' => 'Alvarez',
        'email' => 'molliealvarez@example.com',
    ],
    [
        'firstname' => 'Dianna',
        'surname' => 'Mcbride',
        'age' => 43,
        'email' => 'diannamcbride@example.com',
    ],
    [
        'firstname' => 'Elvira',
        'surname' => 'Mueller',
        'age' => 50,
        'email' => 'elviramueller@example.com',
    ],
    [
        'firstname' => 'Corine',
        'surname' => 'Morton',
        'age' => 35,
    ],
    [
        'firstname' => 'James',
        'surname' => 'Allison',
    ],
    [
        'firstname' => 'Bowen',
        'surname' => 'Kelley',
        'age' => 50,
        'email' => 'bowenkelley@example.com',
    ],
];

$renderer = new ArrayToTextTable($data);
echo $renderer->getTable();

Result

┌───────────┬─────────┬───────────────────────────┬─────┐
│ FIRSTNAME │ SURNAME │           EMAIL           │ AGE │
├───────────┼─────────┼───────────────────────────┼─────┤
│ Mollie    │ Alvarez │ molliealvarez@example.com │     │
│ Dianna    │ Mcbride │ diannamcbride@example.com │ 43  │
│ Elvira    │ Mueller │ elviramueller@example.com │ 50  │
│ Corine    │ Morton  │                           │ 35  │
│ James     │ Allison │                           │     │
│ Bowen     │ Kelley  │ bowenkelley@example.com   │ 50  │
└───────────┴─────────┴───────────────────────────┴─────┘

Options

$renderer->setData($newData);
// Accepts array of arrays and array of objects.
// Can also be called through getTable():
$renderer = new ArrayToTextTable();
echo $renderer->getTable($data);

$renderer->setDecorator(new \Zend\Text\Table\Decorator\Ascii());
// Unicode (default), Ascii, Blank

$renderer->setIndentation("\t");
// default: *empty string*

$renderer->setDisplayKeys(false);
// true, false, 'auto' (default, doesn't display keys if all of them are integers)

$renderer->setUpperKeys(false);
// default: true

$renderer->setKeysAlignment(ArrayToTextTable::AlignCenter);
// AlignLeft, AlignCenter (default), AlignRight

$renderer->setValuesAlignment(ArrayToTextTable::AlignLeft);
// AlignLeft (default), AlignCenter, AlignRight

$formatter = function(&$value, $key, $renderer) {
    if ($value === true)
        $value = 'TRUE';
    else if ($value === false)
        $value = 'FALSE';
    else if ($value === '')
        $value = 'EMPTY';
    else if ($value === null)
        $value = 'NULL';
};
$renderer->setFormatter($formatter);
// default: null

License

This library is published under The MIT License.