php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #79411 Support `break` for array_walk/array_walk_recursive
Submitted: 2020-03-24 08:53 UTC Modified: 2020-03-24 11:08 UTC
Votes:1
Avg. Score:4.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:1 (100.0%)
From: 6562680 at gmail dot com Assigned:
Status: Suspended Package: Scripting Engine problem
PHP Version: 7.3.16 OS: Win10
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please — but make sure to vote on the bug!
Your email address:
MUST BE VALID
Solve the problem:
41 + 10 = ?
Subscribe to this entry?

 
 [2020-03-24 08:53 UTC] 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:
---------------
-


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2020-03-24 09:08 UTC] 6562680 at gmail dot com
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.
 [2020-03-24 11:08 UTC] cmb@php.net
-Status: Open +Status: Suspended
 [2020-03-24 11:08 UTC] cmb@php.net
I think it might make sense to cater to the return value of the
callable, but that certainly needs some discussion regarding the
details and whether this should really be done, given that there
are alternative solutions available right now.  However, this bug
tracker is not suitable for these kind of discussions, so if you,
or anybody else for that matter, is interested in this, please
start the discussion on the internals mailing list[1].

Thanks.

[1] <https://www.php.net/mailing-lists.php#internals>
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Mar 29 07:01:28 2024 UTC