Req #79411 [Opn]: Support `break` for array_walk/array_walk_recursive
| From: | 6562680 at gmail dot com | Date: | Tue, 24 Mar 2020 09:08:57 +0000 |
| Subject: | Req #79411 [Opn]: Support `break` for array_walk/array_walk_recursive | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-226247@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=79411&edit=1
ID: 79411
User updated by: 6562680 at gmail dot com
Reported by: 6562680 at gmail dot com
Summary: Support
break for array_walk/array_walk_recursive
Status: Open
Type: Feature/Change Request
Package: Scripting Engine problem
Operating System: Win10
PHP Version: 7.3.16
Block user comment: N
Private report: N
New Comment:
If few minutes was found solution like that:
```
<?php
$arr = [
0 => [1,2,3],
1 => [4,5,6],
];
$iterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($arr));
var_dump($iterator);
foreach($iterator as $key => $value) {
echo "$key => $value\n";
break;
}
```
I think solved if seems nice.
Previous Comments:
------------------------------------------------------------------------
[2020-03-24 08:53:49] 6562680 at gmail dot com
Description:
------------
Am using array_walk/*_recursive to work with reports (seems like):
```
[
'created' => [],
'updated' => [],
// * => []
]
```
When we have large amount of records we should run 10000 callbacks even if statement
```
static $break = false;
if ($break) return null;
$break = true;
```
was declared
The solution could be
```
class BreakException extends \Exception {}
try {
array_walk_recursive($array, function () {
throw new BreakException();
});
} catch (Exception $e) {
}
```
Would be great to implement some statement like foreach break to avoid building stack
trace while using that feature (because of foreach supports only single level traversing)
Thank you.
Test script:
---------------
-
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=79411&edit=1