-3

Based on the output of the following, I was wondering if anyone could assist me:

  $monthsOfTheYear = range(1, 12);
  print_r ($monthsOfTheYear);

I understand the above process, but what would be the most effective method to recreate this principle with a for loop? I've come up with the following so far:

  $months = ("January", "February", "March", "April", "May", "June", "July", "August", September", "October", "November", "December") {

  for($months{0} = 1, $months{11} = 12, /*????*/) {
      echo "/*????*/";
  } }

If possible I'd like to know how to achieve this within the code for the loop, though I suspect it may require a more complex solution.

4

4 回答 4

1

你想这样做吗?可能是,但我不确定你是否想要这个。请让你的问题更清楚一点。尝试添加示例输出。

$months = array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
for($i = 0;$i < 12; $i++){
 echo $months[$i].'<br>';
}
于 2013-10-23T10:40:22.583 回答
0
for ($i = 0; $i < 12; $i++) {
    echo $monthsOfTheYear[$i];
}
于 2013-10-23T10:40:08.377 回答
0

数十种可能的解决方案之一是

foreach(array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December") as $month) {
    echo $month . "<br>";
}
于 2013-10-23T10:40:40.800 回答
0

你的意思是这样的吗?

$months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];

for ($i=0; $i<count($months); $i++) {
  echo "$months[$i]\n";
}

请参阅http://3v4l.org/T740C上的输出

于 2013-10-23T10:41:12.123 回答