LMSouq
php-dev Open

Pretty-Printing JSON with PHP

ZA
Zach Rattner
1 month ago
3 views
Problem Description
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: $data = array('a' => 'apple', 'b' => 'banana', 'c' => 'catnip'); header('Content-type: text/javascript'); echo json_encode($data); The above code yields the following output: {"a":"apple","b":"banana","c":"catnip"} This is great if you have a small amount of data, but I'd prefer something along these lines: { "a": "apple", "b": "banana", "c": "catnip" } Is there a way to do this in PHP without an ugly hack? It seems like someone at [Facebook][1] figured it out. [1]: https://graph.facebook.com/19292868552

AI-Generated Solution

Powered by LMSouq AI · GPT-4.1-mini

✓ Solution Ready
Analyzing problem and generating solution…
Was this solution helpful?
Back to Knowledge Base