Req #80120 [NEW]: ForEach() backwards support?
| From: | divinity76 at gmail dot com | Date: | Fri, 18 Sep 2020 08:33:35 +0000 |
| Subject: | Req #80120 [NEW]: ForEach() backwards support? | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-229066@lists.php.net to get a copy of this message | ||
From: divinity76 at gmail dot com
Operating system:
PHP version: Next Minor Version
Package: *General Issues
Bug Type: Feature/Change Request
Bug description:ForEach() backwards support?
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?
--
Edit bug report at https://bugs.php.net/bug.php?id=80120&edit=1
--
Fix committed: https://bugs.php.net/fix.php?id=80120&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=80120&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=80120&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=80120&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=80120&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=80120&r=support
Expected behavior: https://bugs.php.net/fix.php?id=80120&r=notwrong
Not enough info: https://bugs.php.net/fix.php?id=80120&r=notenoughinfo
Submitted twice: https://bugs.php.net/fix.php?id=80120&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=80120&r=globals
PHP version support discontinued: https://bugs.php.net/fix.php?id=80120&r=phptooold
Daylight Savings: https://bugs.php.net/fix.php?id=80120&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=80120&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=80120&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=80120&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=80120&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=80120&r=mysqlcfg