我非常确定您可以将while循环放在while循环中,就像我以前做过的那样,但我在这里似乎遇到了一个问题。由于这是相当复杂的,我将发布我的代码,然后解释它。
PHP:
//post vars
$port=$_POST['ports'];
$super=$_POST['super'];
$col=$_POST['columns'];
$row=$_POST['rows'];
//rest of vars
$half_col=$col/$super;
$halfer_col=$col/$super;
$i=1;
$count=1;
$and=1;
$one=1;
$sector=array();
$dat_array=array();
echo $port."<br>";
echo $super."<br>";
echo $col."<br>";
echo $row."<br><br><br><br>";
echo "<h1>here goes nothen</h1><br><br>";
while($count<$port){
while($i<=$half_col){
$dat_array[]=$halfer_col;
$i+=$and;
$halfer_col-=$and;
echo "<br>halfer_col:".$halfer_col."<br>";
}
print_r($dat_array);
$sector[]=implode(',',$dat_array);
$count+=$half_col;
$halfer_col+=$half_col;
}
echo "<br><br>ANNNNNNND...<br><br>";
print_r($sector);现在,这是用户输入端口:24超级:2列:12行:2结果:
24
2
12
2
here goes nothen
halfer_col:5
halfer_col:4
halfer_col:3
halfer_col:2
halfer_col:1
halfer_col:0
Array ( [0] => 6 [1] => 5 [2] => 4 [3] => 3 [4] => 2 [5] => 1 ) Array ( [0] => 6 [1] => 5 [2] => 4 [3] => 3 [4] => 2 [5] => 1 ) Array ( [0] => 6 [1] => 5 [2] => 4 [3] => 3 [4] => 2 [5] => 1 ) Array ( [0] => 6 [1] => 5 [2] => 4 [3] => 3 [4] => 2 [5] => 1 )
ANNNNNNND...
Array ( [0] => 6,5,4,3,2,1 [1] => 6,5,4,3,2,1 [2] => 6,5,4,3,2,1 [3] => 6,5,4,3,2,1 )基本上,它接受用户输入,将其分解并内爆到基于",“的探测器顺序中。然而,据我所知,它并没有在这里添加:$halfer_col+=$half_col;,这导致数据不会增加,有什么想法吗?或者解释为什么这不会增加和影响我的内部while循环的设置方式。
最后,最终的数组应该是这样的:
Array ( [0] => 6,5,4,3,2,1 [1] => 12,11,10,9,8,7 [2] => 18,17,16,15,14,13 [3] => 24,23,22,21,20,19 )发布于 2013-01-23 03:30:52
您可以将代码更改为以下代码:
$port = (int)$_POST['ports'];
$super = (int)$_POST['super'];
$col = (int)$_POST['columns'];
$row = (int)$_POST['rows'];
$colHalf = $col / $super;
$finalArray = array();
for ($i = $port; $i >= 1; $i -= $colHalf) {
$tempArray = array();
for ($j = 0; $j < $colHalf; $j++) {
$tempArray[] = $i - $j;
}
$finalArray[] = implode(",", $tempArray);
}
$finalArray = array_reverse($finalArray);
echo "<pre>" . print_r($finalArray, true) . "</pre>";这将打印您想要的内容(port = 24,super = 2,col = 12,row = 2):
Array
(
[0] => 6,5,4,3,2,1
[1] => 12,11,10,9,8,7
[2] => 18,17,16,15,14,13
[3] => 24,23,22,21,20,19
)发布于 2013-01-23 02:59:08
您可以尝试创建一个外部的while循环。
for(; $count<$port; $count+=$half_col){
while($i<=$half_col){
$dat_array[]=$halfer_col;
$i+=$and;
$halfer_col-=$and;
echo "<br>halfer_col:".$halfer_col."<br>";
}
print_r($dat_array);
$sector[]=implode(',',$dat_array);
$halfer_col+=$half_col;
}https://stackoverflow.com/questions/14465874
复制相似问题