php - Do I need a foreach with MultipleIterator? -
this code comment , write file , work fine.
$comment1 = $xpath->query('//*[comment() = "abc"]'); $comment2 = $xpath->query('//*[comment() = "cde"]'); $commentiterator = new multipleiterator(); $commentiterator->attachiterator(new iteratoriterator($comment1)); $commentiterator->attachiterator(new iteratoriterator($comment2)); foreach ($commentiterator $comments) { file_put_contents('page1.php', $dom->savehtml($comments[0])); file_put_contents('page2.php', $dom->savehtml($comments[1])); } the problem result correct if have file_put_contents outside foreach. $comments local variable inside loop. need foreach?
this code works $comments outside foreach
$comment1 = $xpath->query('//*[comment() = "abc"]'); $comment2 = $xpath->query('//*[comment() = "cde"]'); $commentiterator = new multipleiterator(); $commentiterator->attachiterator(new iteratoriterator($comment1)); $commentiterator->attachiterator(new iteratoriterator($comment2)); foreach ($commentiterator $comments) { } file_put_contents('page1.php', $dom->savehtml($comments[0])); file_put_contents('page2.php', $dom->savehtml($comments[1]));
what if, in case remove foreach? in current example, iterate on if has found atleast 1 child, $comments hold last value got filled last iteration foreach did. if remove foreach completely, $comments won't available anymore.