note 77593 deleted from ref.array by felipe

[email protected]
Newsgroups php.notes
Message-ID <[email protected]>
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]);
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.