【问题标题】:How to make two foreach loops DRY?如何使两个 foreach 循环干燥?
【发布时间】:2021-12-30 21:13:50
【问题描述】:

我想不出一种方法来减少这个循环。每个循环中的处理都是相同的。我会展示我尝试过的东西,但我什至无法弄清楚要尝试什么。

<?

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

【问题讨论】:

    标签: php foreach dry


    【解决方案1】:
    foreach (($xml->entry ? $xml->entry : $rss->channel->item) as $item) {
        ...
    }
    

    【讨论】:

      【解决方案2】:
      $items = $xml->entry ? $xml->entry : $rss->channel->item;
      
      foreach( $items as $item ) {
        ...
      } 
      

      【讨论】:

        猜你喜欢
        • 2018-08-29
        • 1970-01-01
        • 1970-01-01
        • 2022-10-20
        • 1970-01-01
        • 1970-01-01
        • 2022-06-26
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多