Created
March 18, 2012 17:06
-
-
Save sylwit/2077683 to your computer and use it in GitHub Desktop.
Array_map() recursive and closure
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$array_map_recursive = function ($fn, $arr) use(&$array_map_recursive) { | |
$rarr = array (); | |
foreach ( $arr as $k => $v ) { | |
$rarr [$k] = is_array ( $v ) ? $array_map_recursive ( $fn, $v ) : $fn ( $v ); // or call_user_func($fn, $v) | |
} | |
return $rarr; | |
}; | |
ex: $ini = $array_map_recursive('htmlspecialchars_decode', $ini); |
à chaque fois qu'on fait de la récursivité, on pourrait l'écrire en itératif... avec plus de contrôle ;) (enfin, c'est que mon avis !)
Je veux bien voir l'implémentation en itératif ! J'ai peur que ce soit moins lisible.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
C'est joli la programmation fonctionnelle récursive.
J'aurais pas pensé à l'écrire comme ça !!