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 Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: 6562680 at gmail dot com
New email:
PHP Version: OS:

 

 [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

Pull Requests

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 Nov 08 16:01:29 2024 UTC