dqfaom241360 2019-04-25 12:59

# 循环的迭代应该产生16个名字而不是4个

I have the loop below which iterates 4 times. Within this look is another which defines my group names. I would like to produce the output

``````Group 01
Group 02
Group 03
....
Group 15
Group 16
``````

instead of what I currently get

``````Group 01
Group 02
Group 03
Group 04
Group 01
Group 02
Group 03
Group 04
Group 01
Group 02
Group 03
Group 04
Group 01
Group 02
Group 03
Group 04
``````

Code

``````foreach (\$largegroups as \$key => \$users) { // loops 4 times
for (\$i = 1; \$i <= 4; \$i++) {
\$num = sprintf("%02d", \$i);
\$groupname = 'Group ' . \$num;
}
}
``````
• 写回答

#### 3条回答默认 最新

• dragon2025 2019-04-25 13:01
关注

You need to add a counter to the outer loop, and multiply that by 4 before adding `\$i` to get the group number. For example:

``````\$g = 0;
foreach (\$largegroups as \$key => \$users) { // loops 4 times
for (\$i = 1; \$i <= 4; \$i++) {
\$num = sprintf("%02d", \$g * 4 + \$i);
\$groupname = 'Group ' . \$num;
}
\$g++;
}
``````
本回答被题主选为最佳回答 , 对您是否有帮助呢?
评论

#### 悬赏问题

• ¥15 做一个关于单片机的比较难的代码，然后搞一个PPT进行解释
• ¥40 matlab简单代码修改补充
• ¥15 python提取.csv文件中的链接会经常出现爬取失败
• ¥15 数据结构中的数组地址问题
• ¥15 maya的mel里，怎样先选择模型A，然后利用mel脚本自动选择有相同名字的模型B呢。
• ¥15 Python题，根本不会啊
• ¥15 会会信号与系统和python的来
• ¥15 关于#python#的问题
• ¥20 oracle RAC 怎么配置啊，配置
• ¥15 excel 日常使用中出现问题