dougang1965 2014-04-08 09:03
浏览 93

PHP:使用动态变量创建for循环

I'm trying to slim down the code:

I want to make a for loop out of this part, but it wouldn't work.

$line1 = $frage1[0] . '|' . $frage1[1] . '|' . $frage1[2] . '|' . $frage1[3];
$line2 = $frage2[0] . '|' . $frage2[1] . '|' . $frage2[2] . '|' . $frage2[3];
$line3 = $frage3[0] . '|' . $frage3[1] . '|' . $frage3[2] . '|' . $frage3[3];
$line4 = $frage4[0] . '|' . $frage4[1] . '|' . $frage4[2] . '|' . $frage4[3];
$line5 = $frage5[0] . '|' . $frage5[1] . '|' . $frage5[2] . '|' . $frage5[3];

This is my attempt:

for ($i=1; $i<6; $i++){
        ${line.$i} = ${frage.$i}[0] . '|' . ${frage.$i}[1] . '|' . ${frage.$i}[2] . '|' . ${frage.$i}[3];
    }

EDIT: This is the solution that works (just so simple :-p):

for ($i=1; $i<18; $i++){
        ${"line".$i} = implode("|", ${"frage".$i});
        fwrite($antworten, ${"line".$i});
    }
  • 写回答

3条回答 默认 最新

  • douke8473 2014-04-08 09:08
    关注
    for ($i=1; $i<6; $i++){
            $line[$i] = $frage[0] . '|' . $frage[1] . '|' . $frage$i[2] . '|' . $frage$i[3];
        }
    

    This will insert the same thing you posted abov e in line 1 to 6. Is this what you are looking for?!

    评论

报告相同问题?

悬赏问题

  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥50 汇编语言除法溢出问题
  • ¥65 C++实现删除N个数据列表共有的元素
  • ¥15 Visual Studio问题
  • ¥15 state显示变量是字符串形式,但是仍然红色,无法引用,并显示类型不匹配
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波