Created
January 7, 2020 14:24
-
-
Save othyn/a933d8084066cc7fa2cda4d034f032af to your computer and use it in GitHub Desktop.
Quick PHP table output for console
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* A quick way, with a bit of overhead, to get nice tabular output | |
* from a PHP CLI app. | |
* | |
* Sometimes I find myself just requiring very quick and dirty | |
* tabular output and find this to be the path of least resistance. | |
* | |
* This method utilises Symfony's Console's Table and BufferedOutput | |
* classes to generate the table and capture the built table to later | |
* store or output. | |
* | |
* $ composer require symfony/console | |
* | |
* https://symfony.com/doc/current/components/console/helpers/table.html | |
*/ | |
declare(strict_types=1); | |
require 'vendor/autoload.php'; | |
use Symfony\Component\Console\Helper\Table; | |
use Symfony\Component\Console\Output\BufferedOutput; | |
$output = new BufferedOutput(); | |
$table = new Table($output); | |
$table->setHeaders(['First Name', 'Last Name']) | |
->setRows([ | |
['John', 'Smith'], | |
['Jane', 'Doe'] | |
]) | |
->setStyle('default') | |
->render(); | |
echo $output->fetch(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment