如何把php索引变量到php的另一个变量?

  • 本文关键字:php 变量 另一个 索引 php
  • 更新时间 :
  • 英文 :


我有一个starttotime函数在我的php代码。我想把它放到for循环中。通常我们把动态索引附在变量上。但在这里我不能。我不知道为什么?我想把变量$ I而不是下面代码中的1。

$no_col=10;
for ($i=1; $i <=$no_col ; $i++) {
$start_year_1 = date ('Y', strtotime(${"start1"}));
$start_month_1 = date ('m', strtotime(${"start1"}));
}

我想每次从1到$no_col重复一次,像下面这样:

$start_year_2 = date ('Y', strtotime(${"start2"}));
$start_month_2 = date ('m', strtotime(${"start2"}));
...

直接使用动态变量

$no_col=10;
for ($i=1; $i <=$no_col ; $i++) {
// create the variable names
$start_col = "start" . $i;
$start_year_col = "start_year_" . $i;
$start_month_col = "start_month_" . $i;
// then use those strings as variables
$$start_year_col = date ('Y', strtotime($$start_col));
$$start_month_col = date ('m', strtotime($$start_col));
}

最新更新