> What is the best way to delete an element from an array?
> I tried using unset(), and it didn't work.

unset works with arrary elements, but it must be a global variable and not passed in.  
Try the code below to see the results.

<?php
// deletes an element of global array
function deleteGlobal() {
global $animals;
        echo "<br>in deleteGlobal()-Spot<br>\n";
        unset ($animals["Spot"]);
        foreach ($animals as $key => $value) {
                echo "$key: $value<br>\n";
        }
}
// deletes an element in local array only
function deleteElement($annArr) {
        echo "<br>in deleteElement()-Felix<br>\n";
        unset ($annArr["Felix"]);
        foreach ($annArr as $key => $value) {
                echo "$key: $value<br>\n";
        }
}
// 
$animals = array("Kitty"=>"cat", "Max"=>"dog", "Spot"=>"Dog", 
"Felix"=>"cat","Morris"=>"cat");
echo "Initial array<br>\n";
foreach ($animals as $key => $value) {
        echo "$key: $value<br>\n";
}
deleteGlobal();
echo "<br>after deleteGlobal()<br>\n";
foreach ($animals as $key => $value) {
        echo "$key: $value<br>\n";
}
deleteElement($animals);
echo "<br>after deleteElement()<br>\n";
foreach ($animals as $key => $value) {
        echo "$key: $value<br>\n";
}
echo "<br>After unset()-Morris<br>\n";
unset ($animals["Morris"]);
foreach ($animals as $key => $value) {
        echo "$key: $value<br>\n";
}
?>

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]

Reply via email to