php - foreach loop doesn't continue to execute -



php - foreach loop doesn't continue to execute -

i have code removes xml tags expired , list other tags, if 1 tag expired delete , stop foreach loop execution without listing other tags. if reload page after code finished removing expired tag list other tags without problem. how can create go on list other tags? php code:

$xml_file = simplexml_load_file("xml_file.xml"); foreach ($xml_file $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 first tag , print hello twice, removes first tag , stop foreach loop execution without printing thing, , if reload page after deleting first tag prints hello twice without problem.

some lines commented purpose unclear ... can remove, not foreach loop , have start end... otherwise removing chair under - loop unclear, whether should start 'new' $item, or jump on 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 remove kid without conversion dom

$children = &$xml_file->children(); // rest same, replace // $dom = dom_import_simplexml($item); // $dom->parentnode->removechild($dom); // unset($children[$i]);

php foreach

Comments

Popular posts from this blog

xslt - DocBook 5 to PDF transform failing with error: "fo:flow" is missing child elements. Required content model: marker* -

mediawiki - How do I insert tables inside infoboxes on Wikia pages? -

Local Service User Logged into Windows -