title: Format echo json_encode
body: <p>I would like to format the echo json_encode, the output is currently</p>
  
  <pre><code>{"results":{"course":"CC140","books":{"book":[[{"id":"300862","title":"Building object-oriented software","isbn":"0070431965","borrowedcount":"6"}]]}}}
  </code></pre>
  
  <p>Whereas i would like to to output like this:</p>
  
  <pre><code>{
      "results": {
          "course": "CC140",
          "books": {
              "book": [
                  [
                      {
                          "id": "300862",
                          "title": "Building object-oriented software",
                          "isbn": "0070431965",
                          "borrowedcount": "6"
                      }
                  ]
              ]
          }
      }
  }
  </code></pre>
  
  <p>This is the code that makes the JSON</p>
  
  <pre><code>$temp = array();
      foreach ($my_array as $counter =&gt; $bc) {
          $temp['id'] = "$id[$counter]";
          $temp['title'] = "$title[$counter]";
          $temp['isbn'] = "$isbn[$counter]";
          $temp['borrowedcount'] = "$borrowedcount[$counter]";
          $t2[] = $temp;
      }
  
          $data = array(
    "results" =&gt; array(
      "course" =&gt; "$cc",
      "books" =&gt; array(
        "book" =&gt;
        array(  
          $t2
        )
      )
    )
  );
      echo json_encode($data);
  </code></pre>
  
  <p>Any help or pointers would be appreciated, thanks</p>
  
  <p>Adding this</p>
  
  <pre><code>header('Content-type: application/json');
  echo json_encode($data, JSON_PRETTY_PRINT);
  </code></pre>
  
  <p>formats the JSON, but the header also outs the entire HTML document</p>