note 59431 added to function.array-walk-recursive
| From: | cory at lavacube dot com | Date: | Tue, 06 Dec 2005 22:09:58 +0000 |
| Subject: | note 59431 added to function.array-walk-recursive | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-99652@lists.php.net to get a copy of this message | ||
A simple way to implement array_walk_recursive() in PHP 4 is to use to do the following...
<?php
// first, lets define our function
function test_array_walkr( &$item, $key )
{
// do what you want to do here - in this example we will
// check to see if $item is an array. If it is, we will
// check to see if the key '.hide' exists. If this exists,
// we will set the entire array to NULL;
if( is_array($item) && array_key_exists('.hide', $item) )
{
$item = NULL;
}
// this is the code that causes the recursive effect
// we do this after, to allow for any changes to $item
// to be included in the next recursive call...
if( is_array($item) )
{
array_walk($item, __FUNCTION__);
}
}
// next, let's define our array:
$test = array(
'one' => array('one->one', 'one->two',
'one->three'),
'two' => array('.hide'=>true, 'two->one',
'two->two', 'two->three'),
'three' => array('three->one', 'three->two',
'three->three')
);
// lets run the test. ;-)
array_walk($test, 'test_array_walkr');
// ... and get the results
print_r($test);
?>
This example will yeild:
Array
(
[one] => Array
(
[0] => one->one
[1] => one->two
[2] => one->three
)
[two] =>
[three] => Array
(
[0] => three->one
[1] => three->two
[2] => three->three
)
)
Hope this helps someone. :-)
----
Manual Page -- http://www.php.net/manual/en/function.array-walk-recursive.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+59431
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+59431&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+59431&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+59431&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+59431&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+59431&report=yes&reason=non-english
Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+59431&report=yes&reason=already+in+docs
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+59431&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+59431&report=yes
Search -- http://master.php.net/manage/user-notes.php