note 17351 deleted from function.array-slice by jimw
| From: | jimw@php.net | Date: | Mon, 10 Dec 2001 23:29:38 +0000 |
| Subject: | note 17351 deleted from function.array-slice by jimw | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-22463@lists.php.net to get a copy of this message | ||
For array_slice documentation:
Here is a little function that will delete an element from either an index or associative array
(assuming it is one or the other, and that the element only exists once):
function array_delete (&$aArray, $element) {
// Element is key in associative array
if ($aArray[$element] != null) {
$iPos = array_search($element, array_keys($aArray));
array_splice($aArray, $iPos, 1);
}
// Element is value in indexed array
else if (in_array($element, $aArray) == true) {
$iPos = array_search($element, $aArray);
$aBegin = array_slice($aArray, 0, $iPos);
$aEnd = array_slice($aArray, $iPos + 1);
$aArray = array_merge($aBegin, $aEnd);
}
}