login  Naam:   Wachtwoord: 
Registreer je!
 Scripts:

Scripts > PHP > Handige scripts > Recursieve array_map

Recursieve array_map

Auteur: Gerard - 12 augustus 2009 - 11:20 - Gekeurd door: Richard - Hits: 2962 - Aantal punten: (0 stemmen)



Een standaard array_map implementatie geeft je de mogelijkheid om de callback van één functie toe te passen op één of meerdere arrays. Tijdens de ontwikkeling van mijn projecten kwam ik er echter achter dat ik het omgekeerde veel vaker gebruik; het toepassen van meerdere functies op één array. Een ander probleem waar ik tegenaan liep is het toepassen van array_map op een multi-dimensionale array, zoals bijvoorbeeld een formulier waarin je werkt met checkboxes met als naam "id[]".

Om dit probleem op te lossen heb ik een functie geschreven die het dus mogelijk maakt om meerdere functies uit te voeren op alle elementen van een array. Deze functie kan als volgt worden bekeken:

array array_map_recursive (array $array, string $function [, string $function [, string $... ]]);

Een mogelijke implementatie van deze functie kan zijn als je bijvoorbeeld een resultaat hebt uit je database in de variabele $userInfo. Je wilt deze tonen op het scherm, maar eerst wil je natuurlijk nog even wat functies uitvoeren. De meest traditionele aanpak zou de volgende code kunnen opleveren:
  1. <?php
  2.  
  3. echo htmlspecialchars(stripslashes($userInfo['username'])) . '<br />'
  4. . htmlspecialchars(stripslashes($userInfo['email']));


Met het gebruik van array_map zou dit dan terug te brengen zijn tot het volgende:
  1. <?php
  2.  
  3. $userInfo = array_map('stripslashes' , $userInfo);
  4. $userInfo = array_map('htmlspecialchars', $userInfo);
  5.  
  6. echo $userInfo['username'] . '<br />'
  7. . $userInfo['email'];


Of wanneer je beide calls op 1 regel zou zetten:
  1. <?php
  2.  
  3. $userInfo = array_map('htmlspecialchars', array_map('stripslashes', $userInfo));
  4.  
  5. echo $userInfo['username'] . '<br />'
  6. . $userInfo['email'];


Nou dit dit alles met zo'n kort stukje code nog niet echt bijzonder irritant. Wanneer je echter grote arrays gaat verwerken dan kan dit wel eens vervelend worden. Maar zoals gezegd zit het grootste voordeel in de recursieve mogelijkheid van deze functie. Bovenstaande code zou met deze functie er echter als volgt uitzien:
  1. <?php
  2.  
  3. $userInfo = array_map_recursive($userInfo, 'stripslashes', 'htmlspecialchars');
  4.  
  5. echo $userInfo['username'] . '<br />'
  6. . $userInfo['email'];

Code:
  1. <?php
  2.  
  3. /**
  4.   * This function can be used to execute multiple functions and apply the callback to the elements of the
  5.   * given array. The default array_map implementation has support for only 1 function, but can handle
  6.   * several arrays to which the callback has to be applied. This function has support for only a single
  7.   * array, but is able to handle multiple functions. The function also has support for multi-dimensional
  8.   * arrays, as it's a recursive function.
  9.   *
  10.   * @author Gerard Klomp <gerard.klomp@sitemasters.be>
  11.   * @version 1.0
  12.   * @license http://sitemasters.be/mit-license.txt MIT License
  13.   * @param array $array The source array
  14.   * @param string $function The name of the function to be executed
  15.   * @return array An array containing all elements of the source array after applying the callback of each function.
  16.   */
  17. function array_map_recursive($array, $function)
  18. {
  19. $arguments = func_get_args();
  20.  
  21. if (!is_array($arguments[0]) && is_array($arguments[1]))
  22. {
  23. $temporary = $arguments[0];
  24. $arguments[0] = $arguments[1];
  25. $arguments[1] = $temporary;
  26.  
  27. unset($temporary);
  28. }
  29.  
  30. $returnArray = array_shift($arguments);
  31.  
  32. foreach($returnArray as $key => $value)
  33. {
  34. if (is_array($value))
  35. {
  36. array_unshift($arguments, $value);
  37. $value = call_user_func_array(__FUNCTION__, $arguments);
  38. array_shift($arguments);
  39. }
  40. else
  41. {
  42. foreach ($arguments as $function)
  43. {
  44. if (function_exists($function))
  45. {
  46. $value = call_user_func($function, $value);
  47. }
  48. }
  49. }
  50.  
  51. $returnArray[$key] = $value;
  52. }
  53.  
  54. return $returnArray;
  55. }
Download code! Download code (.txt)

 Stemmen
Niet ingelogd.

 Reacties
Post een reactie
Lees de reacties (3)
© 2002-2024 Sitemasters.be - Regels - Laadtijd: 0.059s