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

PHP:如何将PHP数组转换为json格式,php,mysql,arrays,json,Php,Mysql,Arrays,Json,我有一组来自mysql查询的数据,如下所示 hub | month | frequency GALAXY | 10 | 1 GALAXY | 11 | 2 GALAXY | 12 | 1 LEVERAGES | 10 | 3 LEVERAGES | 12 | 2 我想使用json_encode将数据填充为json格式,如下所示: [{"name":"GALAXY","total":"4","articles":[["10",

我有一组来自mysql查询的数据,如下所示

hub       | month | frequency 
GALAXY    | 10    | 1
GALAXY    | 11    | 2 
GALAXY    | 12    | 1 
LEVERAGES | 10    | 3 
LEVERAGES | 12    | 2 
我想使用json_encode将数据填充为json格式,如下所示:

[{"name":"GALAXY","total":"4","articles":[["10","1"],["11","2"],["12","1"]]},{"name":"LEVERAGES","total":"5","articles":[["10","3"],["12","2"]]}]
但我无法获得正确的json。下面是我的代码:

$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);

有人吗?提前感谢。

我认为,您应该添加
$root[]=$main编码到foreach块中,因此每个$main数组都将放入$root矩阵中:

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;
}

另外,循环中还有$hub未定义。因此,$main['name']可能是未定义的。

如上所述,但是如果您想给根目录一个属性,这将允许您保持数据的组织。使用
$root['data']=$main。因此,该守则将成为:

$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);
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);

您的代码中定义了
$hub
在哪里?它看起来像
$main['name']
和其他两个
$main
在每次
迭代中都会被覆盖,所以您只能得到
$root[]
$root['name']
的最后一次迭代结果。@Nikolay是的,完全去掉$root