在php数组中按月分组数据

时间:2018-12-18 15:13:41

标签: php arrays

我有2个这样的数组:

天数组

Array (
    [0] => 2018-01-01
    [1] => 2018-02-04
    [2] => 2018-02-15
    [3] => 2018-04-06
    [4] => 2018-04-12
    [5] => 2018-04-19
    [6] => 2018-04-27
    [7] => 2018-07-08
    [8] => 2018-08-12
    [9] => 2018-08-11
    [10] => 2018-08-21
    [11] => 2018-10-12
    [12] => 2018-10-13
    [13] => 2018-10-14
    [14] => 2018-10-15
    [15] => 2018-11-06
    [16] => 2018-12-12
    [17] => 2018-12-28
)

数据数组

Array (
    [0] => 4
    [1] => 2
    [2] => 3
    [3] => 2
    [4] => 2
    [5] => 9
    [6] => 7
    [7] => 8
    [8] => 12
    [9] => 21
    [10] => 1
    [11] => 2
    [12] => 13
    [13] => 4
    [14] => 15
    [15] => 6
    [16] => 2
    [17] => 8
)

我需要对数据进行分组(在同一月份彼此添加),并且分组的日期也要以月为单位。

预期结果是这样的单个数组:

Array (
    [0] => Array ( [month] => 01 [data] => 4) 
    [1] => Array ( [month] => 02 [data] => 5) /* 2 + 3 = 5 */
    ...
    ...
)

或两个数组一个月,另一个数组用于数据。

这不是重复项。 This answer仅在几个月中只有第一天多次时才起作用。而且也很棘手。

2 个答案:

答案 0 :(得分:2)

您必须遍历数组并创建一个新数组。

<?php
$arr1 = array ('2018-01-01', '2018-02-04', '2018-02-15', '2018-04-06', '2018-04-12', '2018-04-19', '2018-04-27', '2018-07-08', '2018-08-12', '2018-08-11', '2018-08-21', '2018-10-12', '2018-10-13', '2018-10-14', '2018-10-15', '2018-11-06', '2018-12-12', '2018-12-28');

$arr2 = array(4, 2, 3, 2, 2, 9, 7, 8, 12, 21, 1, 2, 13, 4, 15, 6, 2, 8);


$count1 = count($arr1);
$count2 = count($arr2);
$new_arr = array();

while($count1 > 0 and $count2 > 0){
    $count1--;
    $count2--;
    $data = $arr2[$count2];
    $month = $arr1[$count1];
    $month = explode("-", $month);
    $month = $month[1];
    $new_arr[] = ['month' => $month, 'data' => $data];
}

print_r($new_arr);

您可以在这里进行测试:http://sandbox.onlinephpfunctions.com/code/bbf06c48b7242a8d3575610e0cec3ef378e84c4b

编辑:

<?php
$arr1 = array ('2018-01-01', '2018-02-04', '2018-02-15', '2018-04-06', '2018-04-12', '2018-04-19', '2018-04-27', '2018-07-08', '2018-08-12', '2018-08-11', '2018-08-21', '2018-10-12', '2018-10-13', '2018-10-14', '2018-10-15', '2018-11-06', '2018-12-12', '2018-12-28');

$arr2 = array(4, 2, 3, 2, 2, 9, 7, 8, 12, 21, 1, 2, 13, 4, 15, 6, 2, 8);


$count1 = count($arr1);
$count2 = count($arr2);
$new_arr = array();

while($count1 > 0 and $count2 > 0){
    $count1--;
    $count2--;
    $data = $arr2[$count2];
    $month = $arr1[$count1];
    $month = explode("-", $month);
    $month = $month[1];
    $new_arr[] = ['month' => $month, 'data' => $data];
}

$temp = [];
foreach($new_arr as $value) {
    if(!array_key_exists($value['month'], $temp)) {
        $temp[$value['month']] = 0;
    }
    $temp[$value['month']] += $value['data'];
}

print_r($temp);

您可以在这里进行测试:http://sandbox.onlinephpfunctions.com/code/7178c298da8422cf1e8f23ee230adece5999a914

答案 1 :(得分:1)

这是我不同的解决方案:

$array = array();
$var = -1;
$i = 0;

foreach($timestamp as $date) {
    $months = date("F", strtotime($date));
    if(!isset($temp) || $months != $temp) {
        ++$var;
        $array[$var]['data'] = $data[$i];
        $array[$var]['month'] = $months;
        ++$i;
    }
    else {
        $array[$var]['data'] = $array[$var]['data'] + $data[$i];
        ++$i;
    }
    $temp = $months;
}