note 77593 added to ref.array
| From: | tvscoundrel at hotmail dot com | Date: | Wed, 05 Sep 2007 09:52:58 +0000 |
| Subject: | note 77593 added to ref.array | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-130194@lists.php.net to get a copy of this message | ||
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]);
----
Server IP: 87.238.162.147
Probable Submitter: 84.198.173.123
----
Manual Page -- http://www.php.net/manual/en/ref.array.php
Edit -- https://master.php.net/note/edit/77593
Del: integrated -- https://master.php.net/note/delete/77593/integrated
Del: useless -- https://master.php.net/note/delete/77593/useless
Del: bad code -- https://master.php.net/note/delete/77593/bad+code
Del: spam -- https://master.php.net/note/delete/77593/spam
Del: non-english -- https://master.php.net/note/delete/77593/non-english
Del: in docs -- https://master.php.net/note/delete/77593/in+docs
Del: other reasons-- https://master.php.net/note/delete/77593
Reject -- https://master.php.net/note/reject/77593
Search -- https://master.php.net/manage/user-notes.php