[英]add in loop in multi dimensional array
i am facing a problem can some one suggest me 我面临一个问题,有人可以建议我吗
for ($i = 1; $i <= 2; $i++) {
$r2 = 0;
for ($t = 1; $t <= 2; $t++) {
echo $r2;
$r2++
}
}
output is 0101
; 输出为
0101
;
can i get output 0123
??? 我可以得到输出
0123
? please 请
if 如果
for ($i = 1; $i <= 3; $i++) {
$r2 = 0;
for ($t = 1; $t <= 3; $t++) {
echo $r2;
$r2++
}
}
output is 010101
; 输出是
010101
;
can output 012345678
??? 可以输出
012345678
??? please 请
and if 而如果
for ($i = 1; $i <= 4; $i++) {
$r2 = 0;
for ($t = 1; $t <= 4; $t++) {
echo $r2;
$r2++
}
}
output is 01010101
; 输出是
01010101
;
can output 0123456789101112131415
??? 可以输出
0123456789101112131415
??? please i think you understand 请我认为你明白
thanks 谢谢
In all of these cases you are initializing $r2=0; 在所有这些情况下,您都要初始化$ r2 = 0; in the inner loop.
在内部循环中。 It should be outside the loop.
它应该在循环之外。
$r2=0;
for($i=1;$i<=2;$i++){
for($t=1;$t<=2;$t++){
echo $r2;
$r2++
}
}
This would produce "1234". 这将产生“ 1234”。
why are you using two nested for loops ? 为什么要使用两个嵌套的for循环? why not just use one:
为什么不只使用一个:
for ($i=0; $i<=15; $i++) echo $i . " ";
Try this: 尝试这个:
$r2 = 10;
for($t = 0; $t <= $r2; $t++){
echo $r2;
}
Oh wait.. I get it now, why you have the two nested loops, you want to essentially raise a number to the power of 2 in order to control the number of values output. 哦,等等。我现在明白了,为什么要有两个嵌套循环,您实际上想将一个数字提高为2的幂才能控制输出值的数量。 In that case, what you want is simply this:
在这种情况下,您想要的只是这样:
// this is the variable you need to change to affect the number of values outputed
$n = 2;
// Square $n
$m = $n * $n;
// Loop $m times
for ($i = 0; $i < $m; $i++) {
echo $i;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.