JSON Pretty is a very simple library that prints a JSON array in full color with proper indentation.
You can install the package via composer:
composer require rcubitto/json-pretty
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.
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);
composer test
Please see CHANGELOG for more information what has changed recently.
Please see CONTRIBUTING for details.
If you discover any security related issues, please use the issue tracker.
The MIT License (MIT). Please see License File for more information.
This package was generated using the PHP Package Boilerplate.