|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2009-12-09 09:33 UTC] anton at zebooka dot com
Description:
------------
As mentioned in documentation, array items can have either string or int. However keys that are numerical strings are converted to int.
This is how to create an array with item, which key is string, while it is a number.
Reproduce code:
---------------
$a = array(123);
$o = (object) $a;
$o->{0} = 456;
$a = (array) $o;
var_dump($a);
Expected result:
----------------
array(2) {
[0]=>
int(456)
}
Actual result:
--------------
array(2) {
[0]=>
int(123)
["0"]=>
int(456)
}
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sun Nov 30 12:00:01 2025 UTC |
Oh, hell. I know what is written in documentation. But this is clearly a bug. Why can't you open console, type "php -a" and test it your self??? For unbelievers: php > $o = new stdClass(); php > $o->{123} = 456; php > echo $o->{123}; 456 So object HAS!!! numerical properties. php > var_dump($o); object(stdClass)#1 (1) { ["123"]=> int(456) } php > var_dump($a); array(1) { ["123"]=> int(456) } php > error_reporting(-1); php > var_dump($a[123]); Notice: Undefined offset: 123 in php shell code on line 1 NULL There is still no bug??? Are you kidding? PHP team should either disable assigning properties with numberical names to objects (throw ->{} syntax and with (object) cast), or convert objects to arrays and arrays to objects normally, without such bells and candies. :)