note 108183 modified in ref.array by salathe
| From: | salathe@php.net | Date: | Thu, 05 Apr 2012 09:59:11 +0000 |
| Subject: | note 108183 modified in ref.array by salathe | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-186981@lists.php.net to get a copy of this message | ||
If your searching for a function that is deleting all elements in an array with the same value here
is it.
I was searching for something like that but did not find it.
Hope you will like it and use it.
<?php
//deletes all $array entries with $valueToDelete as element value; does function recursive
//without changing original array
public static function array_delete_elements($array = null, $valueToDelete = null) {
//no array given
if (!isset($array)) {
return null;
}
//process
$resultList = array();
foreach ($array as $entryKey => $entryValue) {
if (is_array($entryValue)) {
//entry is array and should be handled again recursive
$resultList[$entryKey] = array_delete_elements($entryValue, $valueToDelete);
} else {
if ($entryValue != $valueToDelete) {
$resultList[$entryKey] = $entryValue;
}
}
}
return $resultList;
}
?>
--was--
If your searching for a function that is deleting all elements in an array with the same key here is
it.
I was searching for something like that but did not find it.
Hope you will like it and use it.
//deletes all $array entries with $valueToDelete as element value; does function recursive
//without changing original array
public static function array_delete_elements($array = null, $valueToDelete = null) {
//no array given
if (!isset($array)) {
return null;
}
//process
$resultList = array();
foreach ($array as $entryKey => $entryValue) {
if (is_array($entryValue)) {
//entry is array and should be handled again recursive
$resultList[$entryKey] = array_delete_elements($entryValue, $valueToDelete);
} else {
if ($entryValue != $valueToDelete) {
$resultList[$entryKey] = $entryValue;
}
}
}
return $resultList;
}
http://php.net/manual/en/ref.array.php