2013-01-22 122 views
0

我很确定你可以在while循环中放置一个while循环,就像我之前做过的一样,但是我似乎在这里遇到了一个问题。由于这是非常复杂的,生病发布我的代码然后解释它。while while 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) 
+1

你想要什么输出? (可能有另一种方法) – h2ooooooo

+1

也许你打算每次都重新启动内部循环,每次都是$ i = 1?就像现在这样,在外循环第一次运行期间,$ i只是小于$ half_col。 –

+0

@ h2ooooooo我添加了最后的打印数组应该看起来像 – Nick

回答

1

你可以更改您的代码如下:

$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>"; 

这将打印你想要什么(端口= 24,超级= 2,COL = 12,列= 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 
) 
+0

非常感谢!你有没有可能说出我的错在哪里? – Nick

0

你可以尝试制作外,同时一个for循环。

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; 
} 
+0

我沉迷于for循环的想法,但我无法想象它的工作方式。我只是这样试了,输出也一样^ – Nick