PHP:如何从此阵列创建Morris Area图表?

时间:2015-09-07 10:34:51

标签: php arrays json morris.js

我正在尝试编写一个函数,它将我的数组转换为json数据,用于Morris Area图表从数据库中提取的原始数组如下:

    Array
(
    [0] => Array
        (
            [tracking_date] => 2015-08-29
            [tracking_isbot] => 0
        )

    [1] => Array
        (
            [tracking_date] => 2015-08-29
            [tracking_isbot] => 0
        )

    [2] => Array
        (
            [tracking_date] => 2015-08-29
            [tracking_isbot] => 0
        )

    [3] => Array
        (
            [tracking_date] => 2015-08-29
            [tracking_isbot] => 0
        )

    [4] => Array
        (
            [tracking_date] => 2015-08-29
            [tracking_isbot] => 0
        )

    [5] => Array
        (
            [tracking_date] => 2015-08-29
            [tracking_isbot] => 0
        )

    [6] => Array
        (
            [tracking_date] => 2015-08-29
            [tracking_isbot] => 0
        )

    [7] => Array
        (
            [tracking_date] => 2015-08-29
            [tracking_isbot] => 1
        )

    [8] => Array
        (
            [tracking_date] => 2015-08-29
            [tracking_isbot] => 1
        )

    [9] => Array
        (
            [tracking_date] => 2015-08-29
            [tracking_isbot] => 1
        )

    [10] => Array
        (
            [tracking_date] => 2015-08-29
            [tracking_isbot] => 1
        )

    [11] => Array
        (
            [tracking_date] => 2015-08-29
            [tracking_isbot] => 0
        )

    [12] => Array
        (
            [tracking_date] => 2015-08-29
            [tracking_isbot] => 0
        )

    [13] => Array
        (
            [tracking_date] => 2015-08-29
            [tracking_isbot] => 1
        )

    [14] => Array
        (
            [tracking_date] => 2015-08-29
            [tracking_isbot] => 1
        )

    [15] => Array
        (
            [tracking_date] => 2015-08-30
            [tracking_isbot] => 0
        )

    [16] => Array
        (
            [tracking_date] => 2015-08-30
            [tracking_isbot] => 0
        )

    [17] => Array
        (
            [tracking_date] => 2015-08-30
            [tracking_isbot] => 0
        )

    [18] => Array
        (
            [tracking_date] => 2015-08-30
            [tracking_isbot] => 0
        )

    [19] => Array
        (
            [tracking_date] => 2015-08-30
            [tracking_isbot] => 0
        )

    [20] => Array
        (
            [tracking_date] => 2015-08-30
            [tracking_isbot] => 0
        )

    [21] => Array
        (
            [tracking_date] => 2015-08-30
            [tracking_isbot] => 0
        )

    [22] => Array
        (
            [tracking_date] => 2015-08-30
            [tracking_isbot] => 0
        )

    [23] => Array
        (
            [tracking_date] => 2015-08-30
            [tracking_isbot] => 0
        )

    [24] => Array
        (
            [tracking_date] => 2015-08-30
            [tracking_isbot] => 0
        )

    [25] => Array
        (
            [tracking_date] => 2015-08-30
            [tracking_isbot] => 0
        )

    [26] => Array
        (
            [tracking_date] => 2015-08-30
            [tracking_isbot] => 1
        )

    [27] => Array
        (
            [tracking_date] => 2015-08-30
            [tracking_isbot] => 1
        )

    [28] => Array
        (
            [tracking_date] => 2015-08-30
            [tracking_isbot] => 1
        )

    [29] => Array
        (
            [tracking_date] => 2015-08-30
            [tracking_isbot] => 0
        )

    [30] => Array
        (
            [tracking_date] => 2015-08-30
            [tracking_isbot] => 0
        )

    [31] => Array
        (
            [tracking_date] => 2015-08-30
            [tracking_isbot] => 0
        )

    [32] => Array
        (
            [tracking_date] => 2015-08-30
            [tracking_isbot] => 0
        )

    [33] => Array
        (
            [tracking_date] => 2015-08-30
            [tracking_isbot] => 1
        )

    [34] => Array
        (
            [tracking_date] => 2015-08-30
            [tracking_isbot] => 0
        )

    [35] => Array
        (
            [tracking_date] => 2015-08-30
            [tracking_isbot] => 0
        )

    [36] => Array
        (
            [tracking_date] => 2015-08-30
            [tracking_isbot] => 1
        )

    [37] => Array
        (
            [tracking_date] => 2015-08-30
            [tracking_isbot] => 0
        )

    [38] => Array
        (
            [tracking_date] => 2015-08-30
            [tracking_isbot] => 1
        )

    [39] => Array
        (
            [tracking_date] => 2015-08-31
            [tracking_isbot] => 0
        )

    [40] => Array
        (
            [tracking_date] => 2015-08-31
            [tracking_isbot] => 0
        )

    [41] => Array
        (
            [tracking_date] => 2015-08-31
            [tracking_isbot] => 0
        )

    [42] => Array
        (
            [tracking_date] => 2015-08-31
            [tracking_isbot] => 1
        )

    [43] => Array
        (
            [tracking_date] => 2015-08-31
            [tracking_isbot] => 0
        )

    [44] => Array
        (
            [tracking_date] => 2015-08-31
            [tracking_isbot] => 0
        )

    [45] => Array
        (
            [tracking_date] => 2015-08-31
            [tracking_isbot] => 0
        )

    [46] => Array
        (
            [tracking_date] => 2015-08-31
            [tracking_isbot] => 1
        )

    [47] => Array
        (
            [tracking_date] => 2015-08-31
            [tracking_isbot] => 1
        )

    [48] => Array
        (
            [tracking_date] => 2015-08-31
            [tracking_isbot] => 1
        )

    [49] => Array
        (
            [tracking_date] => 2015-08-31
            [tracking_isbot] => 0
        )

    [50] => Array
        (
            [tracking_date] => 2015-08-31
            [tracking_isbot] => 0
        )

    [51] => Array
        (
            [tracking_date] => 2015-08-31
            [tracking_isbot] => 0
        )

    [52] => Array
        (
            [tracking_date] => 2015-08-31
            [tracking_isbot] => 0
        )

    [53] => Array
        (
            [tracking_date] => 2015-08-31
            [tracking_isbot] => 0
        )

    [54] => Array
        (
            [tracking_date] => 2015-08-31
            [tracking_isbot] => 1
        )

    [55] => Array
        (
            [tracking_date] => 2015-08-31
            [tracking_isbot] => 1
        )

    [56] => Array
        (
            [tracking_date] => 2015-08-31
            [tracking_isbot] => 1
        )

    [57] => Array
        (
            [tracking_date] => 2015-08-31
            [tracking_isbot] => 1
        )

    [58] => Array
        (
            [tracking_date] => 2015-08-31
            [tracking_isbot] => 0
        )

    [59] => Array
        (
            [tracking_date] => 2015-08-31
            [tracking_isbot] => 0
        )

    [60] => Array
        (
            [tracking_date] => 2015-08-31
            [tracking_isbot] => 0
        )

    [61] => Array
        (
            [tracking_date] => 2015-08-31
            [tracking_isbot] => 0
        )

    [62] => Array
        (
            [tracking_date] => 2015-08-31
            [tracking_isbot] => 0
        )

    [63] => Array
        (
            [tracking_date] => 2015-08-31
            [tracking_isbot] => 0
        )

    [64] => Array
        (
            [tracking_date] => 2015-08-31
            [tracking_isbot] => 0
        )

    [65] => Array
        (
            [tracking_date] => 2015-08-31
            [tracking_isbot] => 1
        )

    [66] => Array
        (
            [tracking_date] => 2015-08-31
            [tracking_isbot] => 1
        )

    [67] => Array
        (
            [tracking_date] => 2015-08-31
            [tracking_isbot] => 1
        )

    [68] => Array
        (
            [tracking_date] => 2015-08-31
            [tracking_isbot] => 0
        )

    [69] => Array
        (
            [tracking_date] => 2015-08-31
            [tracking_isbot] => 0
        )

    [70] => Array
        (
            [tracking_date] => 2015-08-31
            [tracking_isbot] => 0
        )

    [71] => Array
        (
            [tracking_date] => 2015-08-31
            [tracking_isbot] => 0
        )

    [72] => Array
        (
            [tracking_date] => 2015-08-31
            [tracking_isbot] => 0
        )

    [73] => Array
        (
            [tracking_date] => 2015-08-31
            [tracking_isbot] => 1
        )

    [74] => Array
        (
            [tracking_date] => 2015-08-31
            [tracking_isbot] => 0
        )

    [75] => Array
        (
            [tracking_date] => 2015-08-31
            [tracking_isbot] => 0
        )

    [76] => Array
        (
            [tracking_date] => 2015-08-31
            [tracking_isbot] => 0
        )

    [77] => Array
        (
            [tracking_date] => 2015-08-31
            [tracking_isbot] => 1
        )

    [78] => Array
        (
            [tracking_date] => 2015-08-31
            [tracking_isbot] => 1
        )

)

最终的json输出应如下所示:

    { date: '2015-08-29', human: 9, bots: 6 },
    { date: '2015-08-30', human: 18,  bots: 6 },
    { date: '2015-08-31', human: 26,  bots: 14 }

从原始数组:

[date] => xxxx-xx-xx as date:'xxxx-xx-xx'
[isbot] => 0 should be counted as hits from human and
[isbot] => 1 should be counted as hits from bots.

我已创建此功能但无法以所需格式返回数据:

function chartData($results) {
$data = array();

foreach($results as $index => $result) {
    if($result['tracking_isbot'] == 1) {
        if(isset($data[$result['tracking_date']]['bots'])) {
            $data[$result['tracking_date']]['bots'] += 1;
        } else {
            $data[$result['tracking_date']]['bots'] = 1;
        }
    } else {
        if(isset($data[$result['tracking_date']]['human'])) {
            $data[$result['tracking_date']]['human'] += 1;
        } else {
            $data[$result['tracking_date']]['human'] = 1;
        }
    }
}

return $data;
}

上面的函数在json encode:

之前返回数组
Array
(
    [2015-08-29] => Array
        (
            [human] => 9
            [bots] => 6
        )

    [2015-08-30] => Array
        (
            [human] => 18
            [bots] => 6
        )

    [2015-08-31] => Array
        (
            [human] => 26
            [bots] => 14
        )

)

上面数组的json_encode():

{"2015-08-29":{"human":9,"bots":6},"2015-08-30":{"human":18,"bots":6},"2015-08-31":{"human":26,"bots":14}}

2 个答案:

答案 0 :(得分:0)

您还需要保存日期:

function chartData($results) {
    $data = array();

    foreach($results as $result) {
        $t_date = $result['tracking_date'];

        if(!isset($data[$t_date])) {
            $data[$t_date] = array(
                'date' => $t_date,
                'human' => 0,
                'bots' => 0,
            );
        }

        $data[$t_date]['bots']  += $result['tracking_isbot'];
        $data[$t_date]['human'] += (1 - $result['tracking_isbot']);
    }

    return $data;
}

答案 1 :(得分:0)

感谢@Matteo Tassinari,感谢您的解决方案。我稍微修改了你的代码,现在它完全适合我...

function chartData($results) {
$data = array();
$newArray = array();

foreach($results as $result) {
    $t_date = $result['tracking_date'];

    if(!isset($data[$t_date])) {
        $data[$t_date] = array(
            'date' => $t_date,
            'human' => 0,
            'bots' => 0,
        );
    }

    $data[$t_date]['bots']  += $result['tracking_isbot'];
    $data[$t_date]['human'] += (1 - $result['tracking_isbot']);
}

foreach($data as $key => $value) {
    array_push($newArray, $value);
}

return $newArray;
}