Naveen Gamage Naveen Gamage - 5 months ago 15
PHP Question

how to ignore first loop and continue from second in foreach?

I use foreach loop but it always gives an wierd result in the first but others are fine so i want to remove 1st loop and continue from 2nd...

My code is

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

Answer
$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>';
}
}
Comments