Skip to content
Advertisement

Pretty-Printing JSON with PHP

I’m building a PHP script that feeds JSON data to another script. My script builds data into a large associative array, and then outputs the data using json_encode. Here is an example script:

JavaScript

The above code yields the following output:

JavaScript

This is great if you have a small amount of data, but I’d prefer something along these lines:

JavaScript

Is there a way to do this in PHP without an ugly hack? It seems like someone at Facebook figured it out.

Advertisement

Answer

PHP 5.4 offers the JSON_PRETTY_PRINT option for use with the json_encode() call.

http://php.net/manual/en/function.json-encode.php

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement