douwulu2576 2018-06-21 11:18
浏览 19

按月计算数组和组合价格,莫里斯图表

I want to group results by month, to be precise Sum of total prices for each month, but i don't understand how i can divide months, then Sum price for each month.

This is required for morris chart like this

$data2[] = array(
            'y' => $month,
            'a' => price,
            'b' => ''
        );

I was using before query to get this results and was a easy way to do it, but now with code is different story.

Any help with explanation of how each segment of code is work will be so nice, so i can avoid problems like this one in future.

I need something like this :

$month = [May, June]; 
$price = [Sum of all prices for May, Sum of all prices for June];

This is a code :

    $monthsArray = array();

    // get all dates in array
    foreach ($array as $key) {
        // convert dates to short format, month-year -> Jun-18 
        $monthsArray[] = date('M-y', strtotime($key['time']));
    }

    $months = array();
    foreach ($monthsArray as $date) {

        $mon = substr($date, 0, 6);

        if (!in_array($mon, $months)) array_push($months, $mon);
    }

    foreach ($months as $m) {
        // final array for chart
        $data2[] = array('y'=>$m, 'a'=>'', 'b'=>'');
    }

    $morris = json_encode($data2);

This is array i have

Array
(
[0] => Array
    (
        [price] => -1835.25
        [time] => 2018-05-29 16:53:38
    )

[1] => Array
    (
        [price] => -1743.52
        [time] => 2018-05-29 16:53:39
    )

[2] => Array
    (
        [price] => -4445.55
        [time] => 2018-05-31 13:21:04
    )

[3] => Array
    (
        [price] => -34647.04
        [time] => 2018-05-31 18:29:43
    )

[4] => Array
    (
        [price] => 16888.41
        [time] => 2018-06-01 15:05:24
    )

[5] => Array
    (
        [price] => 14369.05
        [time] => 2018-06-07 14:44:21
    )

[6] => Array
    (
        [price] => -49579.69
        [time] => 2018-06-11 09:14:42
    )

[7] => Array
    (
        [price] => -33300.94
        [time] => 2018-06-08 23:50:29
    )

[8] => Array
    (
        [price] => 4413.21
        [time] => 2018-06-12 07:15:52
    )

[9] => Array
    (
        [price] => 2724.69
        [time] => 2018-06-12 07:15:46
    )

[10] => Array
    (
        [price] => 10224.03
        [time] => 2018-06-08 14:00:13
    )

[11] => Array
    (
        [price] => -797.92
        [time] => 2018-06-08 13:54:08
    )

[12] => Array
    (
        [price] => -25157.34
        [time] => 2018-06-11 11:31:31
    )

[13] => Array
    (
        [price] => 2701.6
        [time] => 2018-06-11 14:32:08
    )

[14] => Array
    (
        [price] => 2038.92
        [time] => 2018-06-12 07:15:48
    )

[15] => Array
    (
        [price] => -10541.58
        [time] => 2018-06-15 10:35:58
    )

)

EDIT:

I manage to group months as you can see in updated code and now i am getting only 2 results as i should be, now its just a issue with SUM of data for each month.

This is what i am getting in console 0: {y: "May-18", a: "", b: ""} 1: {y: "Jun-18", a: "", b: ""}

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 基于卷积神经网络的声纹识别
    • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
    • ¥100 为什么这个恒流源电路不能恒流?
    • ¥15 有偿求跨组件数据流路径图
    • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
    • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
    • ¥15 CSAPPattacklab
    • ¥15 一直显示正在等待HID—ISP
    • ¥15 Python turtle 画图
    • ¥15 stm32开发clion时遇到的编译问题