遍历数组并按莫里斯图表按月分组价格

时间:2018-06-21 11:18:40

标签: php morris.js

我想按月对结果进行分组,准确地说是每个月的总价之和,但是我不知道如何划分月份,然后再划分每个月的总价。

对于这种莫里斯图,这是必需的

   var serverManager = new ServerManager();
   var site = serverManager.Sites["SiteName"];
   var app = site.Applications["MyAppName"];
   var config = app.GetWebConfiguration();
   var section = config.GetSection("appSettings");
   var element = section.GetChildElement("mySettingKey");
   element.SetAttributeValue("value", "MyNewValue");
   serverManager.CommitChanges();

我以前在查询之前使用它来获取结果,这是一种简单的方法,但是现在与代码不同了。

任何有关解释每个代码段如何工作的帮助都非常好,这样我以后就可以避免此类问题。

我需要这样的东西:

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

这是一个代码:

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

这是我拥有的数组

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

编辑:

正如您在更新的代码中看到的那样,我设法对月份进行分组,现在我只得到应有的2个结果,现在这只是每个月的数据总和的问题。

这就是我在控制台中得到的 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 ) )

0 个答案:

没有答案