title: PHP: how to convert Php array to json format
body: <p>I have a set of data from mysql query as per below</p>
  
  <pre><code>hub       | month | frequency 
  GALAXY    | 10    | 1
  GALAXY    | 11    | 2 
  GALAXY    | 12    | 1 
  LEVERAGES | 10    | 3 
  LEVERAGES | 12    | 2 
  </code></pre>
  
  <p>and i would like to populate the data to json format using json_encode as this :</p>
  
  <pre><code>[{"name":"GALAXY","total":"4","articles":[["10","1"],["11","2"],["12","1"]]},{"name":"LEVERAGES","total":"5","articles":[["10","3"],["12","2"]]}]
  </code></pre>
  
  <p>But i couldn't get the right json. Below is my code:</p>
  
  <pre><code>$root = array();
  $aColumns = array('hub', 'month', 'frequency');
  $tangos = $this-&gt;Report_Model-&gt;getMonthHubTango();
  
  
      foreach($tangos-&gt;result_array() as $aRow)
                      {
                          $row = array();
                          $total = 0;
  
                          foreach($aColumns as $col)
                          {
                              $row[] = $aRow[$col];
                              $total += $aRow['frequency'];
                                                      $hub = $aRow['hub'];
  
                          }
                          $main['name'] = $hub;
                          $main['total'] = $total;
                          $main['articles'][] = $row;                 
  
                      }
  
                      $root[] = $main;
  echo json_encode($root);
  </code></pre>
  
  <p>Anyone? Thanks in advance..</p>