您好我正试图让我的for循环动态计数向后或向前。 (处理积极到消极和消极到积极)
以下是我的代码,但它似乎无法正常运行。 (我不明白如何使用变量将for循环从+1更改为-1)
function _allbetween($coords1, $coords2) {
$blocks = "";
if ($coords2[0] == 0 || $coords2[1] == 0 || $coords2[2] == 0) {
$temp2 = $coords2[0].",".$coords2[1].",".$coords2[2];
$temp1 = $coords1[0].",".$coords1[1].",".$coords1[2];
$coords1 = explode(",", $temp2);
$coords2 = explode(",", $temp1);
}
for ($i=0; $i<=2; $i++)
{
if (substr($coords2[$i],0,1) == "-") { //if the value is negative
$step[$i] = "--"; //substract the for loop
} else { //else
$step[$i] = "++"; //add the for loop
}
}
for ($i1=$coords1[0]; $i1<=$coords2[0]; $i1.$step[0])
{
for ($i2=$coords1[1]; $i2<=$coords2[1]; $i2.$step[1])
{
for ($i3=$coords1[2]; $i3<=$coords2[2]; $i3.$step[2])
{
$blocks.= $i1.",".$i2.",".$i3."|";
}
}
}
return $blocks;
}
任何人都能看到我做错了什么?
答案 0 :(得分:0)
没关系!
我现在看到php自动处理正值和负值而不将++更改为 -
答案 1 :(得分:0)
也许你正在寻找这个。
for ($i=0; $i<=2; ) // Note the removed $i++ here
{
if (substr($coords2[$i],0,1) == "-") { //if the value is negative
$i--; //$step[$i] = "--"; //substract the for loop
} else { //else
$i++; //$step[$i] = "++"; //add the for loop
}
}