- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在一个循环中有一个循环,如下所示:
//stuff here to determine what $my_var will be
for($i=0;$i<count($my_var);$i++) {
//stuff here to determine what $anothervar will be
for ($y = 1; $y <= $anothervar; $y++) {
//help needed in here
echo $y; //makes it so count starts over each time it goes around
}
}
my_var 将循环一定次数,但并不总是相同的次数。
内循环也是一个随机数。
输出可能如下所示:
1
1,2
2
3
4
5
6
1,2,3
所以在第一个主循环中,内部循环发生了两次。在第6个主循环中,内循环发生了3次。
我想做的是内部循环每次都从 1 开始,我希望它继续计数。所以我希望输出是这样的:
1
1,2
2
3
4
5
6
3,4,5
假设第三个主循环中有一些内部循环,我们将其设为 4 个内部循环,那么输出应如下所示:
1
1,2
2
3
3,4,5,6
4
5
6
7,8,9
如何在循环中进行连续计数?
编辑
这是最终的工作结果:
//stuff here to determine what $my_var will be
$y = 1;
for($i=0;$i<count($my_var);$i++) {
//stuff here to determine what $anothervar will be
for (; $y <= $anothervar; $y++) {
//help needed in here
echo $y; //this now continues to count up instead of starting over each main loop
}
$y = 1;
}
最佳答案
$x = 0;
for($i=0;$i<count($my_var);$i++) {
//stuff here to determine what $anothervar will be
for ($y = 1; $y <= $anothervar; $y++) {
$x++;
echo $x; // now x is incremented every inner loop by 1
}
}
刚刚更改了第一个代码示例的 3 行。
关于php - 在多个 for 循环中不断计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33353916/
如果这不是一个错误,那就是另一个错误。如果不是那样的话,那就是别的东西了。我觉得我的项目已经改变了很多,现在只是试图解决代码签名问题,结果一切都搞砸了。我严格按照说明进行操作,但出现错误,例如当前的“
我不确定是否有一些我不知道的内置变量或规则,或者 make 是否有问题,或者我只是疯了。 对于我的一个项目,我有一个如下的 makefile: CC=g++ CFLAGS=-O3 `libpng-co
我有大约 10 个 div,它们必须不断翻转,每个 div 延迟 3 秒 这个 codrops 链接的最后一个效果是我正在寻找的,但无需单击 div http://tympanus.net/Devel
我如何使用 jQuery 持续运行 PHP 脚本并每秒获取响应,以及将鼠标上的少量数据发送到同一脚本? 我真的必须添加一些随机扩展才能让这么简单的计时器工作吗? 最佳答案 To iterate is
JBoss 4.x EJB 3.0 我见过如下代码(大大简化): @Stateless @TransactionAttribute(TransactionAttributeType.NOT_SUPPO
使用 PHPStorm,我试图忽略每次尝试进行 git 提交时 pop 的 workspace.xml。 我的 .gitignore 看起来像: /.idea/ .idea/workspace.xml
我是一名优秀的程序员,十分优秀!