deleteFromArray
Auteur: Bart - 16 mei 2009 - 00:40 - Gekeurd door: Wim - Hits: 2827 - 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: |
<?php
error_reporting(E_ALL); // full error reporting
/* -----------------------
* @function deleteFromArray
* @param array (array) array that will be searched for value
* @param remove (array) array with values that will be deleted
* @return newarray (array) array without the remove values
* @notes deletefromarray will search the specific array for values given by
remove param and delete those keys
* @author Bart Johan Dongelmans (quicktimer@home.nl)
* @date May 26th 2009
----------------------- */
function deleteFromArray(&$array, &$remove) // initializing function
{
$array = array_flip($array); // flip the array, so keys will be values and values will be keys
for($i = 0; $i < count($remove); $i++) // loop trough items that will be deleted
{
unset($array[$remove[$i]]); // delete every key that is in remove param
}
$array = array_flip($array); // flip back the array so you'll get the old array as output
RETURN $array;
}
$aFruit = array("citroen", "appel", "sinaasappel", "peer");
$aRemove = array("appel", "citroen");
$aExample = deleteFromArray($aFruit, $aRemove);
print_r($aExample); // returns "sinaasappel" and "peer"
?>
<?php /* ----------------------- * @function deleteFromArray * @param array (array) array that will be searched for value * @param remove (array) array with values that will be deleted * @return newarray (array) array without the remove values * @notes deletefromarray will search the specific array for values given by remove param and delete those keys * @author Bart Johan Dongelmans (quicktimer@home.nl) * @date May 26th 2009 ----------------------- */ function deleteFromArray(&$array, &$remove) // initializing function { $array = array_flip($array); // flip the array, so keys will be values and values will be keys for($i = 0; $i < count($remove); $i++) // loop trough items that will be deleted { unset($array[$remove[$i]]); // delete every key that is in remove param } $array = array_flip($array); // flip back the array so you'll get the old array as output RETURN $array; } $aFruit = array("citroen", "appel", "sinaasappel", "peer"); $aRemove = array("appel", "citroen"); $aExample = deleteFromArray($aFruit, $aRemove); print_r($aExample); // returns "sinaasappel" and "peer" ?>
Download code (.txt)
|
|
Stemmen |
Niet ingelogd. |
|