note 112857 added to function.array-map
| From: | contatoatwilliamsantanadotcomdotbr | Date: | Tue, 30 Jul 2013 12:42:36 +0000 |
| Subject: | note 112857 added to function.array-map | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-195821@lists.php.net to get a copy of this message | ||
In case of you need to recursively bypass a function over the itens of an array, you can use it
<?php
function array_map_recursive($callback, $array) {
foreach ($array as $key => $value) {
if (is_array($array[$key])) {
$array[$key] = array_map_recursive($callback, $array[$key]);
}
else {
$array[$key] = call_user_func($callback, $array[$key]);
}
}
return $array;
}
?>
-----------------------------------------------------------------------
<?php
$strings = array(
'The',
array(
'quick',
'fox',
array(
'brown',
'jumps',
array(
'over',
array(
'the',
array(
'lazy',
array(
'dog'
)
)
)
)
)
)
);
print_r($strings);
$hashedString = array_map_recursive('md5', $strings);
print_r($hashedString);
?>
------------------------------------------------------------------------
Testing it, you'll obtain
<?php
/* Original array */
array (
0 => 'The',
1 =>
array (
0 => 'quick',
1 => 'fox',
2 =>
array (
0 => 'brown',
1 => 'jumps',
2 =>
array (
0 => 'over',
1 =>
array (
0 => 'the',
1 =>
array (
0 => 'lazy',
1 =>
array (
0 => 'dog',
),
),
),
),
),
),
);
/* Recursived array */
array (
0 => 'a4704fd35f0308287f2937ba3eccf5fe',
1 =>
array (
0 => '1df3746a4728276afdc24f828186f73a',
1 => '2b95d1f09b8b66c5c43622a4d9ec9a04',
2 =>
array (
0 => '6ff47afa5dc7daa42cc705a03fca8a9b',
1 => '55947829059f255e4ba2f536a2ae99fe',
2 =>
array (
0 => '3b759a9ca80234563d87672350659b2b',
1 =>
array (
0 => '8fc42c6ddf9966db3b09e84365034357',
1 =>
array (
0 => '0ffe34b4e04c2b282c5a388b1ad8aa7a',
1 =>
array (
0 => '06d80eb0c50b49a509b49f2424e8c805',
),
),
),
),
),
),
);
?>
Hope it helps you.
Cheers.
----
Server IP: 69.147.83.199
Probable Submitter: 177.43.119.65
----
Manual Page -- http://php.net/manual/en/function.array-map.php
Edit -- https://master.php.net/note/edit/112857
Del: integrated -- https://master.php.net/note/delete/112857/integrated
Del: useless -- https://master.php.net/note/delete/112857/useless
Del: bad code -- https://master.php.net/note/delete/112857/bad+code
Del: spam -- https://master.php.net/note/delete/112857/spam
Del: non-english -- https://master.php.net/note/delete/112857/non-english
Del: in docs -- https://master.php.net/note/delete/112857/in+docs
Del: other reasons-- https://master.php.net/note/delete/112857
Reject -- https://master.php.net/note/reject/112857
Search -- https://master.php.net/manage/user-notes.php