login  Naam:   Wachtwoord: 
Registreer je!
 Scripts:

Scripts > PHP > Snippets > deleteFromArray

deleteFromArray

Auteur: Bart - 16 mei 2009 - 00:40 - Gekeurd door: Wim - Hits: 2757 - Aantal punten: 4.25 (2 stemmen)




Deze functie zoekt in een array naar een bepaalde waarde en verwijdert deze uit de array. Verdere uitleg staat in het script.

Code:
  1. <?php
  2. error_reporting(E_ALL); // full error reporting
  3.  
  4. /* -----------------------
  5. * @function deleteFromArray
  6. * @param array (array) array that will be searched for value
  7. * @param remove (array) array with values that will be deleted
  8. * @return newarray (array) array without the remove values
  9. * @notes deletefromarray will search the specific array for values given by
  10.   remove param and delete those keys
  11. * @author Bart Johan Dongelmans (quicktimer@home.nl)
  12. * @date May 26th 2009
  13. ----------------------- */
  14.  
  15. function deleteFromArray(&$array, &$remove) // initializing function
  16. {
  17. $array = array_flip($array); // flip the array, so keys will be values and values will be keys
  18.  
  19. for($i = 0; $i < count($remove); $i++) // loop trough items that will be deleted
  20. {
  21. unset($array[$remove[$i]]); // delete every key that is in remove param
  22. }
  23.  
  24. $array = array_flip($array); // flip back the array so you'll get the old array as output
  25.  
  26. RETURN $array;
  27. }
  28.  
  29. $aFruit = array("citroen", "appel", "sinaasappel", "peer");
  30.  
  31. $aRemove = array("appel", "citroen");
  32.  
  33. $aExample = deleteFromArray($aFruit, $aRemove);
  34.  
  35. print_r($aExample); // returns "sinaasappel" and "peer"
  36. ?>
Download code! Download code (.txt)

 Stemmen
Niet ingelogd.

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