|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2011-03-11 04:52 UTC] aharvey@php.net
-Status: Open
+Status: Duplicate
[2011-03-11 04:52 UTC] aharvey@php.net
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Mon Oct 27 05:00:01 2025 UTC |
Description: ------------ It is well known that PHP casts numeric string indexes to integers, and it is normally impossible to force such indexes to remain strings. For example, the script below will output "integer": $test = array("123" => 1); echo gettype(key($test)); // integer However, it is possible to achieve this if we assign numeric string property to an object (by using the ->{} syntax), and then cast it to array. Not only will the index retain it's string type, but it will also be impossible to access that index with [], which can't be correct behaviour. See attached example. Test script: --------------- <?php $test = new stdClass(); $test->{"123"} = 1; $test = (array)$test; $key = key($test); echo gettype($key); echo "\n"; echo $test[$key]; ?> Expected result: ---------------- integer 1 Actual result: -------------- string PHP Notice: Undefined index: 123 in test.php on line 9