从数组中的当前月开始循​​环循环

时间:2017-04-21 10:03:28

标签: php arrays

我每个月都有一个降雨量的阵列。我在foreach循环中进行一些计算,该循环从数组中的0索引开始。我如何从当月(比如第4个月)开始到明年第3个月结束?以下是我手头的代码。

public static function getWaterLevels($params)
{
    $rainfall = $params["rainfall"];
    $currentTime = Carbon::now();
    $currentMonth = $currentTime->month;
    $volume = 0;
    $demand = $params["household_number"] * 140 * 30;
    $tankSize = $params["tank_capacity"];
    $waterLevels = array();
    foreach ($rainfall as $amount){
        $runoffAmount = 0.80 * ($amount - 2) * $params["roof_area"];
        $volume = $volume + ($runoffAmount - $demand);
        if ($volume > $tankSize)
        {
            $overflow = $volume - $tankSize;
            $deficit = 0;
            $volume = $volume - $overflow;
        }
        elseif ($volume < 0)
        {
            $deficit = abs($volume);
            $overflow = 0;
            $volume = 0;
        }
        else
        {
            $overflow = 0;
            $deficit = 0;
        }
        array_push($waterLevels, array(
            "volume" => $volume,
            "overflow" => $overflow,
            "deficit" => $deficit
        ));
    }
    return $waterLevels;
} 

在上面的函数中,我想在$ currentMonth开始循环 - 1.降雨数组的结构是这样的:

array:12 [
  0 => 59
  1 => 57
  2 => 54
  3 => 76
  4 => 76
  5 => 87
  6 => 82
  7 => 83
  8 => 88
  9 => 91
  10 => 84
  11 => 74
]

0 个答案:

没有答案
相关问题