note 52463 deleted from function.unset by betz
| From: | betz@php.net | Date: | Tue, 03 May 2005 07:36:59 +0000 |
| Subject: | note 52463 deleted from function.unset by betz | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-88887@lists.php.net to get a copy of this message | ||
Note Submitter: geoff at hoffmans dot net
Reason: useless
----
If you want to remove a value from the middle of a linear array, you have to use the zero-based
integer pointer to it, not the string you want to unset:
<?php
$a = array("a","b","c","d");
unset($a["b"]); // has no effect
var_dump($a);
?>
// prints array("a","b","c","d")
<?php
$a = array("a","b","c","d");
unset($a[1]);
var_dump($a);
?>
// prints array("a","c","d");
Conversely, if you have an associative array, unset requires the string key:
<?php
$a =
array("a"=>"abc","b"=>"cbs","c"=>"nbc","d"=>"fox");
unset($a[1]); // has no effect
var_dump($a);
?>
// prints
array("a"=>"abc","b"=>"cbs","c"=>"nbc","d"=>"fox")
<?php
$a =
array("a"=>"abc","b"=>"cbs","c"=>"nbc","d"=>"fox");
unset($a["b"]);
var_dump($a);
?>
// prints
array("a"=>"abc","c"=>"nbc","d"=>"fox")