|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2020-09-18 16:20 UTC] requinix@php.net
-Status: Open
+Status: Feedback
[2020-09-18 16:20 UTC] requinix@php.net
[2020-09-18 17:19 UTC] divinity76 at gmail dot com
[2020-09-18 19:59 UTC] requinix@php.net
-Status: Feedback
+Status: Not a bug
[2020-09-18 19:59 UTC] requinix@php.net
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sat Nov 08 11:00:01 2025 UTC |
Description: ------------ There are situations where one want to iterate a list backwards, I was recently in such a situation, writing: <?php $removeWhitespaceTextNodes = function (\DOMNode $node) use (&$removeWhitespaceTextNodes): void { if ($node->hasChildNodes()) { // Warning: it's important to do it backwards; if you do it forwards, the index for DOMNodeList might become invalidated; // that's why i don't use ForEach() - don't change it (unless you know what you're doing, ofc) for ($i = $node->childNodes->length - 1; $i >= 0; --$i) { $removeWhitespaceTextNodes($node->childNodes->item($i)); } } if ($node->nodeType === XML_TEXT_NODE && !$node->hasChildNodes() && !$node->hasAttributes() && (strlen(trim($node->textContent)) === 0)) { $node->parentNode->removeChild($node); } }; ?> I think it could be useful if ForEach() could iterate stuff backwards, maybe something like: <?php ForEach($iterable as $index=>$val, $flags = 0){} ?> (with a new FOREACH_BACKWARDS flag) or maybe just <?php ForEach($iterable as $index=>$val, bool $backwards = false){} ?> or something like that?