我要把它放到数据库中,例如:
附魔:enchant_id 1,1 str,6 dex。
这就是为什么我在循环它。
我得到了我想要的结果,但有更好的方法吗?
<?php
$str = 1;
$dex = 1;
while ($dex <= 7) {
echo "Str: $str";
echo " - ";
echo "Dex: $dex";
echo "<br>";
$dex++;
if($dex == 8) {
echo "<br>";
$str = 2;
$dex = 1;
while ($dex <= 7) {
echo "Str: $str";
echo " - ";
echo "Dex: $dex";
echo "<br>";
$dex++;
if($dex == 8) {
echo "<br>";
$str = 3;
$dex = 1;
while ($dex <= 7) {
echo "Str: $str";
echo " - ";
echo "Dex: $dex";
echo "<br>";
$dex++;
}
}
}
}
}
?>
输出:
Str: 1 - Dex: 1
Str: 1 - Dex: 2
Str: 1 - Dex: 3
Str: 1 - Dex: 4
Str: 1 - Dex: 5
Str: 1 - Dex: 6
Str: 1 - Dex: 7
Str: 2 - Dex: 1
Str: 2 - Dex: 2
Str: 2 - Dex: 3
Str: 2 - Dex: 4
Str: 2 - Dex: 5
Str: 2 - Dex: 6
Str: 2 - Dex: 7
Str: 3 - Dex: 1
Str: 3 - Dex: 2
Str: 3 - Dex: 3
Str: 3 - Dex: 4
Str: 3 - Dex: 5
Str: 3 - Dex: 6
Str: 3 - Dex: 7
如何简化代码以获得相同的结果?
答案 0 :(得分:1)
您展示的示例输出可以通过简单的嵌套for
循环来实现。例如:
for ($str = 1; $str <= 7; $str++) {
for ($dex = 1; $dex <= 7; $dex++) {
echo "Str: $str - Dex: $dex<br />";
}
echo "<br />"; // For the extra space between sets
}
查看所需输出时的关键是识别模式。左侧有一个递增值(STR),右侧有一个递增值(DEX)。这是两个循环。模式是这样的,对于左侧(外部循环)的每一次增加,右侧(内部循环)有一整套值。