title: Changing the output of json_encode in Codeigniter
body: <p>I am outputting the data in <code>JSON</code> format from MySQL database in <strong>controller</strong> like this, </p>
  
  <pre><code>function byId(){
          $this -&gt; load -&gt; model('usermodel');
          $data['user'] = $this -&gt; usermodel -&gt; getById($this-&gt;uri-&gt;slash_segment(3));
          $this -&gt; output -&gt; set_content_type('application/json');
          $this -&gt; output -&gt; set_output(json_encode($data));
      }
  </code></pre>
  
  <p>Model:</p>
  
  <pre><code>function getById($id){
          $this-&gt;db-&gt;select('*');
          $this-&gt;db-&gt;from('members');
          $this-&gt;db-&gt;where('id', $id);
  
          $q = $this -&gt; db -&gt; get();
  
          if ($q -&gt; num_rows() &gt; 0) {
              foreach ($q-&gt;result() as $row) {
                  $data[] = $row;
              }
              return $data;
          }
  
      }
  </code></pre>
  
  <p>It outputs the data like this,</p>
  
  <p><code>{"user":[{"id":"3","firstname":"Frances","lastname":"Johnson","address":"489 Summit Lane","state":"Minnesota","country":"US","email":"fjohnson@teklist.net","phone":null,"experience":"2","designation":"Script"}]}</code></p>
  
  <p>But I need it like this,</p>
  
  <p><code>{user : {....} }</code></p>
  
  <p>Basically I want to get rid of square brackets.</p>
  
  <p>What can I change in my code to get the intended output?</p>