php foreach里面的变量

时间:2017-12-17 19:41:48

标签: php foreach

我试图遍历txt2的记录,但是如果我把 foreach 放在txt2之外,我只得到一组记录,那就是问题。 获取记录输出的简单方法是什么?

....
int r1 = ran.nextInt(6) + 1; 
int r2 = ran.nextInt(6) + 1;
int r3 = ran.nextInt(6) + 1;
....
do{
 r1 = ran.nextInt(6) + 1; // again random at the start of do-while loop is necessary
 r2 = ran.nextInt(6) + 1;
 r3 = ran.nextInt(6) + 1;
....
}.....

1 个答案:

答案 0 :(得分:2)

不要将foreach放在字符串中,只是与.=连接:

$txt2 = "";
foreach($queryRecords as $res)
{
$code = $res['code'];
$txt2 .= "
NCWEEKE.exe -n=C:/WW4/$wrtID/$wrtID-$code-el.mpr
NCWEEKE.exe -n=C:/WW4/$wrtID/$wrtID-$code-er.mpr
NCWEEKE.exe -n=C:/WW4/$wrtID/$wrtID-$code-tb.mpr
NCWEEKE.exe -n=C:/WW4/$wrtID/$wrtID-$code-dc.mpr";
}

如果您不需要将它们分开,只需将它们连接成一个$txt

$txt = "
@ECHO OFF
@ECHO **** Run NC-Generator WOODWOP 4.0 ****";

foreach($queryRecords as $res)
{
$code = $res['code'];
$txt .= "
NCWEEKE.exe -n=C:/WW4/$wrtID/$wrtID-$code-el.mpr
NCWEEKE.exe -n=C:/WW4/$wrtID/$wrtID-$code-er.mpr
NCWEEKE.exe -n=C:/WW4/$wrtID/$wrtID-$code-tb.mpr
NCWEEKE.exe -n=C:/WW4/$wrtID/$wrtID-$code-dc.mpr";
}
$txt .= "
@ECHO **** Done ****
";
fwrite($batfile, $txt);