rcubitto / json-pretty
Pretty print your PHP arrays with style – in full color and with proper indentation
Installs: 41 029
Dependents: 0
Suggesters: 0
Security: 0
Stars: 2
Watchers: 0
Forks: 2
Open Issues: 2
Requires
- php: ^7.3|^8.0
Requires (Dev)
- phpunit/phpunit: ^9.5
README
JSON Pretty is a very simple library that prints a JSON array in full color with proper indentation.
Installation
You can install the package via composer:
composer require rcubitto/json-pretty
Usage
You can print any array, both sequencial and associate.
\Rcubitto\JsonPretty\JsonPretty::print([ 'store' => 'Best Buy', 'number' => 30305, 'products' => [ [ 'name' => 'TV', 'cost' => 2000.00, 'in_stock' => true ], [ 'name' => 'Phone', 'cost' => 350.80, 'in_stock' => false ], [ 'name' => 'Sample', 'cost' => 0, 'in_stock' => null ] ] ]);
The previous snippet will return the following string:
You can also print an object class:
$obj = new \Stdclass; $obj->prop = 1; $obj->another = 2; \Rcubitto\JsonPretty\JsonPretty::print($obj);
Output:
NOTE: the string is not echoed out, just returned. So if you want to actually echo it out, don't forget to do so.
Config
You can also configure different colors for different types.
$options = [ 'colors' => [ 'bracket' => 'blue', // {}[] and object keys 'number' => 'green', // any is_numeric (except strings like "1") 'string' => 'purple', 'boolean' => 'pink', 'null' => 'black' ] ]; \Rcubitto\JsonPretty\JsonPretty::print($sample, $options);
Testing
composer test
Changelog
Please see CHANGELOG for more information what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security
If you discover any security related issues, please use the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.
PHP Package Boilerplate
This package was generated using the PHP Package Boilerplate.