note 70366 added to function.key
| From: | jeroenschellekens at osu1 dot php dot net | Date: | Fri, 13 Oct 2006 09:35:11 +0000 |
| Subject: | note 70366 added to function.key | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-118484@lists.php.net to get a copy of this message | ||
<?php
$arr = array(
0 => "katie",
"0" => "john",
"0 " => "suzy"
);
foreach($arr as $k => $v) {
print "[{$k}] => $v : Key " . (is_string($k) ? "is " : "is not") .
" a string<br>";
}
?>
outputs:
[0] => john : Key is not a string
[0 ] => suzy : Key is a string
which means PHP treats "0" the same as 0 (both being a numeric entry in the array) and in
this case declares entry 0 twice (initally as katie, then overwrites the same entry with john).
I don't know whether this is a bug or not, but PHP automatically converts strings containing
integers to integers, which in effect delivers a numeric entry in the array instead of an
associative, which can be annoying in some situations.
when you prepend / append any non-numeric character to the key (in my example "suzy"), it
remains an associative entry in the array though.
----
Server IP: 83.137.20.120
Probable Submitter: 217.166.147.50
----
Manual Page -- http://www.php.net/manual/en/function.key.php
Edit -- https://master.php.net/note/edit/70366
Del: integrated -- https://master.php.net/note/delete/70366/integrated
Del: useless -- https://master.php.net/note/delete/70366/useless
Del: bad code -- https://master.php.net/note/delete/70366/bad+code
Del: spam -- https://master.php.net/note/delete/70366/spam
Del: non-english -- https://master.php.net/note/delete/70366/non-english
Del: in docs -- https://master.php.net/note/delete/70366/in+docs
Del: other reasons-- https://master.php.net/note/delete/70366
Reject -- https://master.php.net/note/reject/70366
Search -- https://master.php.net/manage/user-notes.php