#50423 [Opn->Bgs]: Numerical keys casted as strings in arrays, when converting from object
| From: | jani@php.net | Date: | Wed, 09 Dec 2009 12:16:32 +0000 |
| Subject: | #50423 [Opn->Bgs]: Numerical keys casted as strings in arrays, when converting from object | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-145576@lists.php.net to get a copy of this message | ||
ID: 50423
Updated by: jani@php.net
Reported By: anton at zebooka dot com
-Status: Open
+Status: Bogus
Bug Type: Arrays related
Operating System: Ubuntu 9.10
PHP Version: 5.2.11
New Comment:
"If an object is converted to an array , the result is an array whose
elements are the object 's properties. The keys are the member variable
names, with a few notable exceptions: integer properties are
unaccessible;"
And this is exactly what happens. No bug here.
Previous Comments:
------------------------------------------------------------------------
[2009-12-09 09:47:48] johannes@php.net
sorry misread it
------------------------------------------------------------------------
[2009-12-09 09:45:17] anton at zebooka dot com
Yes, I can't. But I have!!!
Look closer.
Expected result is, when I convert object with numerially named
properties, to get array with int keys. But instead I get an array with
string numerical keys
------------------------------------------------------------------------
[2009-12-09 09:41:15] johannes@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to
report
a bug at http://bugs.php.net/how-to-report.php
You can\'t have array keys with numeric strings as key.
------------------------------------------------------------------------
[2009-12-09 09:36:10] anton at zebooka dot com
Again, if you then do echo $a[0]; you'll get 123 instead of 456.
Even if you call arsort($a); the result will be the same - 123.
------------------------------------------------------------------------
[2009-12-09 09:33:43] 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)
}
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=50423&edit=1