note 70366 deleted from function.key by bobby
| From: | bobby@php.net | Date: | Fri, 13 Oct 2006 15:13:32 +0000 |
| Subject: | note 70366 deleted from function.key by bobby | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-118499@lists.php.net to get a copy of this message | ||
Note Submitter: jeroen schellekens
Reason: in docs
----
<?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.