PHP:如何将Php数组转换为json格式

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..

I think, you should add $root[] = $main; into foreach block, so every $main array will be put into $root matrix:

foreach($tangos->result_array() as $aRow)

{
    $row = array();
    $total = 0;

    foreach($aColumns as $col)
    {
        $row[] = $aRow[$col];
        $total += $aRow['frequency'];

    }
    $main['name'] = $hub;
    $main['total'] = $total;
    $main['articles'][] = $row;                 
    $root[] = $main;
}

Plus, you have $hub undefined in loop. So, $main['name'] will probably be undefined.

$root = array();
$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'];

                        }
                        $main['name'] = $hub;
                        $main['total'] = $total;
                        $main['articles'][] = $row;                 
                        $root[] = $main;
                    }


echo json_encode($root);

Just Like described above, however if you want to give the root an attribute, which would allow you to keep your data organised. use $root['data'] = $main;. Therefore the code would become:

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['data'] = $main;
echo json_encode($root);