Home > front end >  How to make two foreach loops DRY?
How to make two foreach loops DRY?

Time:12-31

I cannot figure out a way to cut this down to one loop. The processing in each loop is identical. I'd show what I've tried but I can't even get my head around what to try.

<?

if($xml->entry){
  foreach ($xml->entry as $item) {
    ...
  }
else{
  foreach($rss->channel->item as $item) {
    ...
  }
}

CodePudding user response:

foreach (($xml->entry ? $xml->entry : $rss->channel->item) as $item) {
    ...
}

CodePudding user response:

$items = $xml->entry ? $xml->entry : $rss->channel->item;

foreach( $items as $item ) {
  ...
} 
  • Related