PHP code example of unmainsys / array-to-text-table

1. Go to this page and download the library: Download unmainsys/array-to-text-table library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

unmainsys / array-to-text-table example snippets


use Unmainsys\Util\ArrayToTextTable;

$data = [
    [
        'firstname' => 'Mollie',
        'surname' => 'Alvarez',
        'email' => '[email protected]',
    ],
    [
        'firstname' => 'Dianna',
        'surname' => 'Mcbride',
        'age' => 43,
        'email' => '[email protected]',
    ],
    [
        'firstname' => 'Elvira',
        'surname' => 'Mueller',
        'age' => 50,
        'email' => '[email protected]',
    ],
    [
        'firstname' => 'Corine',
        'surname' => 'Morton',
        'age' => 35,
    ],
    [
        'firstname' => 'James',
        'surname' => 'Allison',
    ],
    [
        'firstname' => 'Bowen',
        'surname' => 'Kelley',
        'age' => 50,
        'email' => '[email protected]',
    ],
];

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

$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 \Unmainsys\Table\Decorator\Ascii());
// Unicode (default), Ascii

$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