php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #50423 Numerical keys casted as strings in arrays, when converting from object
Submitted: 2009-12-09 09:33 UTC Modified: 2009-12-09 17:40 UTC
From: anton at zebooka dot com Assigned:
Status: Not a bug Package: Arrays related
PHP Version: 5.2.11 OS: Ubuntu 9.10
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: anton at zebooka dot com
New email:
PHP Version: OS:

 

 [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)
}

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-12-09 09:36 UTC] 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:41 UTC] 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:45 UTC] 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:47 UTC] johannes@php.net
sorry misread it
 [2009-12-09 12:16 UTC] jani@php.net
"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.


 [2009-12-09 17:38 UTC] anton at zebooka dot com
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.

:)
 [2009-12-09 17:40 UTC] anton at zebooka dot com
sorry, "throw" == "through"
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Jun 18 11:01:31 2024 UTC