douan6931 2015-07-24 03:57
浏览 81
已采纳

使用foreach中的每个循环在现有数组中创建新数组

I have a foreach loop which, for each pension ID, i am using the pension amount that corresponds to that ID and applying an inflation rate to the pension value over 30 years. There will always be a variable amount of unique pensions for each person.

In the foreach loop I am able to run it and have it produce the future value of the pension amount over time, iterating each years increased values. The problem i am running into is that for each pension id it is adding the new pension values into the name array.

For example, if the individual has 4 pensions, that produces 120 array values in one array. What I am wanting is 4 (or how many pensions are there) arrays with each unique pension values in its own array. My goal is to then add each pension year from each array together and come to a total pension amount for the individual for each year.

Example.
pension 1 array([1] => 100 [2] => 200 [3] => 300…….
pension 2 array([1] => 200 [2]=> 300 [3] => 400……

(then adding each array together based on key)
Total pension amount array([1] => 300 [2] => 500 [3] => 700

I have been trying all sorts of things but can’t find a solution, Im thinking the foreach might not be able to accomplish this.

To to recap the solution I need, after each pass of the foreach I need to create a new array with the new values and not have them add to the existing array.

Any help would be wonderful.

Here is my existing code.

note: everything is going into $pension[]

foreach($pen_start as $key => $value){

    if($value < $age){
        if($pen_cola[$key] == 'Yes'){
            $i = 0;
            $a = $age;
            $previous = $pen_amount[$key];
            while($i <= 29 ){
            $pension[] = str_replace(',','',number_format(($previous*1.02),2));
            $previous = $previous*1.02;
            $i++;
            $a++;
            };
            // if yes end
        } else if($pen_cola[$key] == 'No' || $pen_cola[$key] == ''){

            $i = 0;
            $a = $age;
            $previous = $pen_amount[$key];
            while($i <= 29 ){

            $pension[] = str_replace(',','',number_format(($previous),2));
            $previous = $previous;
            $i++;
            $a++;
            };
        } // if no end
    } // if older end
    else if($value > $age){
        if($pen_cola[$key] == 'Yes'){
            $i = 0;
            $a = $age;
            $previous = 0;
            while($i <= 29 ){

                if($a < $value){
                    $amount = 0;
                }else if($previous == 0){
                    $amount = $pen_amount[$key];}
                    else {$amount = $previous;}

            $pension[] = str_replace(',','',number_format(($amount*1.02),2));
            $previous = $amount*1.02;
            $i++;
            $a++;
            };
            // end if yes
        }  else if($pen_cola[$key] == 'Yes'){
            $i = 0;
            $a = $age;
            $previous = 0;
            while($i <= 29 ){

                if($a < $value){
                    $amount = 0;
                }else if($previous == 0){
                    $amount = $pen_amount[$key];}
                    else {$amount = $previous;}

            $pension[] = str_replace(',','',number_format(($amount),2));
            $previous = $amount;
            $i++;
            $a++;
            };
        } //end if no
    } // if younger end
} // end foreach
  • 写回答

1条回答 默认 最新

  • douhan8610 2015-07-24 04:53
    关注

    Maybe your problem is solved when you use the key in $pension:

    $pensions[$key][] = str_replace(',','',number_format(($amount),2));
    

    If I understand it right, you want to do this:

    $result = array(); //or in php 5.5+ []
    foreach($pensions as $pension){
       foreach($pension as $key => $amount){
           if(!isset($result[$key])){
              $result[$key] = 0;
           }
           $result[$key] += $amount;
       }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何解决y_true和y_predict数据类型不匹配的问题(相关搜索:机器学习)
  • ¥15 PB中矩阵文本型数据的总计问题。
  • ¥40 宿舍管理系统设计(c#)
  • ¥15 MATLAB卫星二体模型仿真
  • ¥15 怎么让数码管亮的同时让led执行流水灯代码
  • ¥20 SAP HANA SQL Script 。如何判断字段值包含某个字符串
  • ¥85 cmd批处理参数如果含有双引号,该如何传入?
  • ¥15 fx2n系列plc的自控成型机模拟
  • ¥15 时间序列LSTM模型归回预测代码问题
  • ¥50 使用CUDA如何高效的做并行化处理,是否可以多个分段同时进行匹配计算处理?目前数据传输速度有些慢,如何提高速度,使用gdrcopy是否可行?请给出具体意见。