带有返回 12 个值(月)的表的 for 循环


For loop with table which return 12 values (month)

当我尝试返回从 1 到 12 的 12 个数字时,我编写了这段代码:

public function getMonths() 
{ 
    for($monthNum = 1; $monthNum <= 12; $monthNum++)
        { 
          $month[$monthNum]=$monthNum;
        } 
return [$month]; 
}

如何返回这 12 个数字?我现在的第一个返回值为零。有人知道如何解决这个问题吗?我只需要 12 个数字而没有 0?

我认为你想写return $month;而不是return [$month],否则你会在返回它时将你的$month数组包装在另一个数组中。