title: Pretty-Printing JSON with PHP
body: <p>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 <code>json_encode</code>. Here is an example script:</p>
  
  <pre><code>$data = array('a' =&gt; 'apple', 'b' =&gt; 'banana', 'c' =&gt; 'catnip');
  header('Content-type: text/javascript');
  echo json_encode($data);
  </code></pre>
  
  <p>The above code yields the following output:</p>
  
  <pre><code>{"a":"apple","b":"banana","c":"catnip"}
  </code></pre>
  
  <p>This is great if you have a small amount of data, but I'd prefer something along these lines: </p>
  
  <pre><code>{
      "a": "apple",
      "b": "banana",
      "c": "catnip"
  }
  </code></pre>
  
  <p>Is there a way to do this in PHP without an ugly hack? It seems like someone at <a href="https://graph.facebook.com/19292868552" rel="noreferrer">Facebook</a> figured it out.</p>