The php function json_encode() has an option to set indentation in the outputted json for a "pretty" version. This option is called: JSON_PRETTY_PRINT
A minor pet peeve is that this feature uses 4 spaces in said indentation.
Is there is a way to make it use 2 spaces instead of 4 or an efficient way to process the output to reduce the 4 spaces to 2 - without it breaking any of the json keys/values which may have spaces in them.
Try this:
$data = ['some' => 'data'];
$json = preg_replace_callback ('/^ +/m', function ($m) {
return str_repeat (' ', strlen ($m[0]) / 2);
}, json_encode ($data, JSON_PRETTY_PRINT));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With