0

我有几乎可以做到这一点的代码......

//get the row info
$Row1 = $FullTable->find('div[class=ismPitchRow1]',0);  
$Row2 = $FullTable->find('div[class=ismPitchRow2]',0);  
$Row3 = $FullTable->find('div[class=ismPitchRow3]',0);  
$Row4 = $FullTable->find('div[class=ismPitchRow4]',0);  
$Row5 = $FullTable->find('div[class=ismPitchRow5]',0);  

//Loop 5 times. One for each row on the pitch.
for ($i=1; $i<=5; $i++)
    {
    if ($i = 1) { echo $Row1; }
    if ($i = 2) { echo $Row2; }  
    if ($i = 3) { echo $Row3; }
    if ($i = 4) { echo $Row4; }
    if ($i = 5) { echo $Row5; }
    }

它可以工作,但正如您所见,它效率不高,设计也很糟糕。我将如何简化这一点?我知道有很多更小的方法可以完成这种循环。

谢谢。

4

2 回答 2

1

使用数组的伟大发明:

//get the row info
$Row[1] = $FullTable->find('div[class=ismPitchRow1]',0);  
$Row[2] = $FullTable->find('div[class=ismPitchRow2]',0);  
$Row[3] = $FullTable->find('div[class=ismPitchRow3]',0);  

或者,更聪明...

for ($i = 1; $i <= 5; $i++) {

    $find = "div[class=ismPitchRow$i]"; 
    $Row[$i] = $FullTable->find($find,0);

}

对回显做同样的事情:

for ($i = 1; $i <= 5; $i++) {

    echo $Row[$i];

}

但为什么不在 1 个循环中做所有事情呢?

for ($i = 1; $i <= 5; $i++) {

    $find = "div[class=ismPitchRow$i]"; 
    echo $FullTable->find($find,0);

}
于 2013-03-26T00:41:57.303 回答
1

我将存储$Row1$Row5an 中array并使用 foreach 循环遍历它。

<?php
$YourArray = array(); 

array_push($YourArray,$FullTable->find('div[class=ismPitchRow1]',0),$FullTable->find('div[class=ismPitchRow2]',0),$FullTable->find('div[class=ismPitchRow3]',0),$FullTable->find('div[class=ismPitchRow4]',0),$FullTable->find('div[class=ismPitchRow5]',0));



foreach($YourArray as $row){
echo $row;
}
?>
于 2013-03-26T00:42:46.823 回答