Req #79411 [Opn->Sus]: Support `break` for array_walk/array_walk_recursive

From: Date: Tue, 24 Mar 2020 11:08:24 +0000
Subject: Req #79411 [Opn->Sus]: Support `break` for array_walk/array_walk_recursive
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-226248@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 Updated by: cmb@php.net Reported by: 6562680 at gmail dot com Summary: Support break for array_walk/array_walk_recursive -Status: Open +Status: Suspended 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: 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> Previous Comments: ------------------------------------------------------------------------ [2020-03-24 09:08:56] 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 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

« previous php.bugs (#226248) next »