note 77593 deleted from ref.array by felipe
| From: | felipe@php.net | Date: | Sun, 30 Dec 2007 19:30:25 +0000 |
| Subject: | note 77593 deleted from ref.array by felipe | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-134534@lists.php.net to get a copy of this message | ||
Note Submitter: tvscoundrel at hotmail dot com
----
phamphong_tn's function to remove an element from an array seems a bit complicated for
something that is actually quite simple.
Here's another way to remove an element from an array:
<?php
//the function
function removeArrayElement(&$arr, $index){
if(isset($arr[$index])){
array_splice($arr, $index, 1);
}
}
//testing the function
$test = array("zero", "one", "two", "three");
echo "<pre>";
print_r($test);
echo "</pre>";
$this->removeArrayElement($test, 2);
echo "<pre>";
print_r($test);
echo "</pre>";
?>
output:
Array
(
[0] => zero
[1] => one
[2] => two
[3] => three
)
Array
(
[0] => zero
[1] => one
[2] => three
)
To remove an element from an associative array is even simpler:
unset($arr[$index]);