note 59431 deleted from function.array-walk-recursive by googleguy
| From: | googleguy@php.net | Date: | Tue, 22 Oct 2013 14:54:59 +0000 |
| Subject: | note 59431 deleted from function.array-walk-recursive by googleguy | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-197232@lists.php.net to get a copy of this message | ||
Note Submitter: cory at lavacube dot com
----
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. :-)