Edit report at http://bugs.php.net/bug.php?id=53039&edit=1
ID: 53039 Updated by: cataphr...@php.net Reported by: monty at dontspam dot tamu dot edu Summary: Extend array_walk_recursive to provide context information to the callback func -Status: Open +Status: Wont fix Type: Feature/Change Request Package: Arrays related Operating System: Windows Server PHP Version: 5.3.3 Block user comment: N New Comment: The RecursiveIteratorIterator, besides not working just with arrays, already provides such functionality. $a = array(1, array(2.1, 2.2)); $it = new RecursiveIteratorIterator(new RecursiveArrayIterator($a)); foreach ($it as $el) { echo $el, " depth ", $it->getDepth(), "\n"; } Your suggestion of using a variable where to store this information is also very inadvisable, if anything, it could be an extra argument passed to the callback function. Previous Comments: ------------------------------------------------------------------------ [2010-10-10 23:21:16] monty at dontspam dot tamu dot edu Description: ------------ It is easy to extend the array_walk_recursive function to be more useful in non- trivial situations which require information about depth & path in the array structure the cursor is in, by adding an optional 4th parameter. This would eliminate the need for MANY user-written depth first search traversal recursive functions. Which consequently makes PHP a more attractive language for people who fear recursion, but need a tree walk function which provides contextual information to the callback. bool array_walk_recursive ( array &$input , callback $funcname [, mixed $userdata ] [, array $state] ) where $state is a data dictionary having any number of context relevant data e.g. the depth of recursion (int), and the keys traversed to arrive at this node (a 1- dimensional list array of string). This enhancement is low risk: it would not break backward-compatibility. This enhancement is high reward: it would make Xpath-like operations more approachable to the average developer. This enhancement would simplify user code: which tends to increase security for websites. Test script: --------------- var $state = Array( $depth => 0 ,$path => Array() ); Expected result: ---------------- N/A Actual result: -------------- N/A ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/bug.php?id=53039&edit=1