note 41955 deleted from function.array-walk by aidan
| From: | aidan@php.net | Date: | Sun, 11 Jul 2004 02:07:24 +0000 |
| Subject: | note 41955 deleted from function.array-walk by aidan | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-73025@lists.php.net to get a copy of this message | ||
Note Submitter: shadedecho
----
in response to "phpContrib (A T) esurfers d o t c o m":
Your code is basically correct, I have tested it because I needed something very similar, basically
to have it traverse the array recursively, calling the callback function only at the leaf-nodes, not
on the sub-indexes of the array.
However, I'd suggest one thing in particular to your example:
the "recursive" call to array_walk() should go ahead and use the same $prefix variable
that was passed into test_alter(), rather than manually specifying 'fruit'.
Like this:
function test_alter (&$item1, $key, $prefix) {
if(is_array($item1)) {
array_walk ($item1, 'test_alter', $prefix); // <-- changed
return;
}
$item1 = "$prefix: $item1";
}
***************************
Also, though in this example it makes little (if any) sense, and is probably self-obvious to some
readers, I wanted to point out for others that you COULD use the same concept for a callback that
you DO want to act on each node of an array, instead of only doing an operation on string nodes
(like if you were printing out the "type"/"value" of each node in an array,
kinda like var_dump() does), whether its string, array, or whatever...
you'd simply remove the return statement in the if() construct, and then decide if you want the
callback "recursive" call for sub-arrays to happen before or after the processing for that
particular node...
in other words, to borrow terminology from binary trees, think of the multi-dimensional array as a
sort of "unary tree" (only one child per node, instead of 2, or more). In that context,
then you decide if you want:
1. a "pre-order traversal" - meaning you process the logic on the node, and THEN call the
"recursive" callback if the item happens to itself be a sub-array. This would be a
top-down ordering of processing for each branch.
function test_alter (&$item1, $key) {
/*
code block where you put if/switch statements, or
any other type (in/)dependent processing logic
for the node, such as echo'ing something out like
var_dump() does, for instance.
*/
// now call the "recursive" call back if the node happens
// to be a sub-array.
if(is_array($item1)) array_walk ($item1, 'test_alter');
}
-OR-
2. a "post-order traversal" - means you first call the recursive callback function, which
effectively processes any part of the branch "below" the current node FIRST, and THEN you
process the current node. This gives a bottom-up ordering of processing for each branch.
function test_alter (&$item1, $key) {
// first call the "recursive" call back if the node happens
// to be a sub-array.
if(is_array($item1)) array_walk ($item1, 'test_alter');
// we can be satisified now that all the processing is done
// for the part of the branch "below" this node (if there
// is any), so now we can process this node.
/*
code block where you put if/switch statements, or
any other type (in/)dependent processing logic
for the node.
*/
}