the most efficient way to remove an element from an array
Posted: Thu Sep 02, 2004 5:49 am
Hello
Assume I have the following array:
Now I would like to remove the ith element from the array (for example, the 2nd element which is 'dd')
Here is a simple way to do this (and probably not so efficient):
I am sure someone can find a more efficient solution (as the size of the array may be huge)
regards
Assume I have the following array:
Code: Select all
$arr = Array('aa', 'dd', 'bb', 'cc');Here is a simple way to do this (and probably not so efficient):
Code: Select all
$value = $arr[1];
$newArr = Array();
foreach ($arr as $element) {
if ($element != $value) {
$newArr [] = $element;
}
}
$arr = $newArr;regards