I have a set of data from mysql query as per below
hub | month | frequency
GALAXY | 10 | 1
GALAXY | 11 | 2
GALAXY | 12 | 1
LEVERAGES | 10 | 3
LEVERAGES | 12 | 2
and i would like to populate the data to json format using json_encode as this :
[{"name":"GALAXY","total":"4","articles":[["10","1"],["11","2"],["12","1"]]},{"name":"LEVERAGES","total":"5","articles":[["10","3"],["12","2"]]}]
But i couldn't get the right json. Below is my code:
$root = array();
$aColumns = array('hub', 'month', 'frequency');
$tangos = $this->Report_Model->getMonthHubTango();
foreach($tangos->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);
Anyone? Thanks in advance..