2012-05-05 69 views
2

我使用foreach循環,但它總是給人一種奇怪的結果,在第一,但其他人都很好,所以我想刪除第一環和第二次繼續...如何忽略第一個循環並從foreach繼續?

我的代碼是

foreach($doc->getElementsByTagName('a') as $a){ 
foreach($a->getElementsByTagName('img') as $img){ 
    echo $a->getAttribute('href'); 
    echo $img->src . '<br>'; 
} 
} 
+1

使用計數器和跳完成的第一個 – Drewdin

+0

!謝謝你,朋友! –

+0

沒問題,我必須自己多次做同樣的事情! – Drewdin

回答

6
$counter = 0; 

foreach($doc->getElementsByTagName('a') as $a){ 
foreach($a->getElementsByTagName('img') as $img){ 

    if ($counter++ == 1) continue; 

    echo $a->getAttribute('href'); 
    echo $img->src . '<br>'; 
} 
} 
1
$nm = 0; 
foreach($doc->getElementsByTagName('a') as $a){ 
    if($nm == 1){ 
    foreach($a->getElementsByTagName('img') as $img){ 
     echo $a->getAttribute('href'); 
     echo $img->src . '<br>'; 
    } 
    } 
    $nm=1; 
} 
+0

thnks 4花時間幫助我兄弟! –

1

嘗試這樣的事情

foreach($doc->getElementsByTagName('a') as $a) 
{ 
    $count = 0; 
    foreach($a->getElementsByTagName('img') as $img) 
    { 
     if(count == 0) 
     { 
      $count++; 
      continue; 
     } 
     echo $a->getAttribute('href'); 
     echo $img->src . '<br>'; 
    } 
} 
+0

thnks 4花時間幫助我兄弟! –

+0

很高興爲您效勞。我想我們大多數人在同一時間內給你類似的解決方案... – swapnilsarwe

11

我可以T的最簡單方法,以跳過第一循環hink是通過使用標誌

例如:

$b = false; 
foreach(...) { 
    if(!$b) {  //edited for accuracy 
     $b = true; 
     continue; 
    } 
} 
+0

thnks 4花時間幫助我兄弟!任何時候都歡迎來到 –

+0

:) –