简体   繁体   中英

foreach loop doesn't continue to execute

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.

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]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM