0

我正在尝试使用从数据库中检索到的动态值来呈现 Highcharts 列明细。

我有一个这样的 php 查询:

$query = mssql_query("SELECT count(create_at) as create,  month(create_at) as month, year(create_at) as year
                FROM tblCrew 
                WHERE create_at >=  '01 JUL 2011' AND create_at <=  '31 JUL 2013'
                GROUP BY  month(create_at), year(create_at) 
                ORDER BY  year(create_at) ASC, month(create_at) ASC ");

为了得到结果,我像这样循环它:

while ($data = mssql_fetch_array($query))
            {
                if (!isset($case[$data['year']])) {
                                $case[$data['year']] = array(
                                    'name' => $data['year'],
                                    'data' => array()
                                );
                    }

                    $case[$data['year']]['data'][] = array(
                        $data['month'],
                        $data['create']
                    );

            }

这是结果:

$case = Array([2011] => Array
    (
        [name] => 2011
        [data] => Array
            (
                [0] => Array ( [0] => JUL [1] => 166 )
                [1] => Array ( [0] => AUG [1] => 144 )
                ........
                .......
                [5] => Array ( [0] => DEC [1] => 124 )
            )
    )

[2012] => Array
    (
        [name] => 2012
        [data] => Array
            (
                [0] => Array ( [0] => JAN [1] => 143 )
                [1] => Array ( [0] => FEB [1] => 177 )
                ........
                .......
                [5] => Array ( [0] => DEC [1] => 132 )
            )
    )       

[2013] => Array
    (
        [name] => 2013
        [data] => Array
            (
                [0] => Array ( [0] => JAN [1] => 166 )
                ........
                .......
                [5] => Array ( [0] => JUL [1] => 124 )
            )
    )       

)

现在我正在尝试添加另一个数组,我将在其中汇总每年的数据并将其放入另一个数组中。就像下面的例子。

$total[] = Array[{name: '2011', total: '1520'}, {name: '2012', total: '2241'}, {name: '2013', total: '1864'}]   
4

1 回答 1

0

您可以添加另一个数组,在初始化的同时初始化它,$case[$data['year']]并在循环结果时添加值:

$total = array();
while ($data = mssql_fetch_array($query))
        {
            if (!isset($case[$data['year']])) {
                            $case[$data['year']] = array(
                                'name' => $data['year'],
                                'data' => array()
                            );
                            $total[$data['year']] = array(
                                'name' => $data['year'],
                                'total' => 0
                            );
                }

                $case[$data['year']]['data'][] = array(
                    $data['month'],
                    $data['create']
                );
                $total[$data['year']]['total'] += $data['create'];

        }
print_r($total);
于 2014-07-08T09:49:32.683 回答