dougan7523 2014-10-16 05:56
浏览 234
已采纳

foreach循环不会继续执行

I have a code that removes only the xml tags that expired and list the other tags, but if one tag expired it will delete it and stop the foreach loop execution without listing the other tags. if I reload the page after the code finished removing the expired tag it will list the other tags normally without any problem. how can I make it continue to list the other tags?
php code:

$xml_file = simplexml_load_file("xml_file.xml");
            foreach ($xml_file as $item)
                {
                    $current_date = time();
                    $article_date = (int)$item->date;
                    $item_number = (int)str_replace("a" , "" ,$item->getName());
                    if ($current_date >= $article_date + 100000)
                        {
                            if ($item->children()->getName() == "a")
                                {
                                    $dom = dom_import_simplexml($item);
                                    $dom->parentNode->removeChild($dom);
                                    $return = simplexml_import_dom($dom);
                                    $xml_file->asXML('xml_file.xml');
                                    unlink('file.html');
                                }
                        }
                    elseif ($current_date < $article_date + 100000)
                        {
                            echo 'hello';
                        }
                }

xml code:

<articles>
<a1><a>gr</a><date>14</date></a1>
<a2><a>gr</a><date>1414141414141414</date></a2>
<a3><a>gr</a><date>1414141414141414</date></a3></articles>

this code should remove the first tag and print hello twice, but it just removes the first tag and stop the foreach loop execution without printing any thing, and if I reload the page after deleting the first tag it prints hello twice without any problem.

  • 写回答

1条回答 默认 最新

  • douquanhui5735 2014-10-16 06:16
    关注

    Some lines are commented as their purpose is unclear ... You can remove, but not with foreach loop and you have to start from the end... otherwise it is like removing the chair under yourself - loop is unclear, whether it should start with the 'new' $item, or jump over it.

    $children = $xml_file->children(); 
    for($i = count($children) - 1; $i >= 0; $i--)
    { 
      $item = $children[$i];
      $current_date = time();
      $article_date = (int)$item->date;
      $item_number = (int)str_replace("a" , "" ,$item->getName());
      if ($current_date >= $article_date + 100000)
      {
         if ($item->children()->getName() == "a")
         {
           $dom = dom_import_simplexml($item);
           $dom->parentNode->removeChild($dom);
          //  $return = simplexml_import_dom($dom);
          //  $xml_file->asXML('xml_file.xml');
          //  unlink('file.html');
         }
      }
      elseif ($current_date < $article_date + 100000)
      {
         echo 'hello';
      }
    }
    
    var_dump($xml_file);
    

    Another way to remove child without conversion to DOM is

    $children = &$xml_file->children();
    // the rest is the same, but replace
    // $dom = dom_import_simplexml($item);
    // $dom->parentNode->removeChild($dom);
    // with
    unset($children[$i]);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算