首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >用PHP合并一定范围内的数组数据

用PHP合并一定范围内的数组数据
EN

Stack Overflow用户
提问于 2018-06-16 03:16:52
回答 1查看 33关注 0票数 0

我有这个数组:

Array
(
    [0] => Array
        (
            [Sum] => 125.00
            [PastDays] => 530
        )

    [1] => Array
        (
            [Sum] => 123.00
            [PastDays] => 110
        )

    [2] => Array
        (
            [Sum] => 500.00
            [PastDays] => 5
        )

    [3] => Array
        (
            [Sum] => 500.00
            [PastDays] => 1
        )

)

我将合并这些数据,使其在基于PAY_Nb_Days的数组上具有此类型

如果

  • PAY_Nb_Days60.

  • If PAY_Nb_Days之间,31 PAY_Nb_Days61之间,并且90.

  • If 31在<PAY_Nb_Days>D20和121.

< PAY_Nb_Days 之间,则D24高于

所以,大概是这样的:

Array
(
    ['Between 0 and 30'] => Array
        (
            [Sum] => 1000.00
        )

    ['Between 31 and 60'] => Array
        (
            [Sum] => 0.00
        )

    ['Between 61 and 90'] => Array
        (
            [Sum] => 0.00
        )

    ['Between 91 and 120'] => Array
        (
            [Sum] => 123.00
        )

    ['Hight than 121'] => Array
        (
            [Sum] => 125.00
        )

)

你能告诉我从哪里开始吗?

下面是我尝试过的:

foreach($items as $item) {
    if($item[PastDays] <= 30) { $sum_0_to_30 += $item['Sum']; } 
    if($item[PastDays] >= 31 && $item[PastDays] <= 60) { $sum_31_to_60 += $item['Sum']; }   
    if($item[PastDays] >= 61 && $item[PastDays] <= 90) { $sum_61_to_90 += $item['Sum']; }   
    if($item[PastDays] >= 91 && $item[PastDays] <= 120) { $sum_91_to_120 += $item['Sum']; } 
    if($item[PastDays] >= 121) { $sum_121 += $item['Sum']; }    
}

谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-16 04:24:15

$arr = [
   'Between 0 and 30'   => ['Sum' => 0],
    'Between 31 and 60'  => ['Sum' => 0],
    'Between 61 and 90'  => ['Sum' => 0],
    'Between 91 and 120' => ['Sum' => 0],
    'Hight than 121'     => ['Sum' => 0],
];





   foreach($items as $item){
         switch($item['PastDays']){
             case ($item['PastDays'] < 31):
                 $arr['Between 0 and 30']['Sum'] += $item['Sum'];
                 break;
             case ($item['PastDays'] < 61):
                 $arr['Between 31 and 60']['Sum'] += $item['Sum'];
                 break;
             case ($item['PastDays'] < 91):
                 $arr['Between 61 and 90']['Sum'] += $item['Sum'];
                 break;
             case ($item['PastDays'] < 121):
                 $arr['Between 91 and 120']['Sum'] += $item['Sum'];
                 break;
             default:
                 $arr['Hight than 121']['Sum'] += $item['Sum'];
                 break;
          }
    }

$arr将包含您需要的信息

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50881528

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档