php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #72254 get_object_vars() returns unusable result when keys are numeric
Submitted: 2016-05-23 00:47 UTC Modified: 2016-11-14 18:33 UTC
Votes:2
Avg. Score:3.0 ± 0.0
Reproduced:2 of 2 (100.0%)
Same Version:0 (0.0%)
Same OS:2 (100.0%)
From: unirgy at gmail dot com Assigned: ajf (profile)
Status: Closed Package: Scripting Engine problem
PHP Version: 7.0.6 OS: Any
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: unirgy at gmail dot com
New email:
PHP Version: OS:

 

 [2016-05-23 00:47 UTC] unirgy at gmail dot com
Description:
------------
If argument in get_object_vars() has numeric keys, the resulting array is built using numeric string keys, which apparently can't be referenced.

Test script:
---------------
$json = '{"1":"A"}';

$arrWorks = json_decode($json, true);
var_dump($arrWorks);
var_dump($arrWorks[1], $arrWorks['1']);

echo "\n=================\n";

$arrDoesntWork = get_object_vars(json_decode($json));
var_dump($arrDoesntWork);
var_dump($arrDoesntWork[1], $arrDoesntWork['1']);

Expected result:
----------------
array(1) {
  [1]=>
  string(1) "A"
}
string(1) "A"
string(1) "A"

=================
array(1) {
  [1]=>
  string(1) "A"
}
string(1) "A"
string(1) "A"

Actual result:
--------------
array(1) {
  [1]=>
  string(1) "A"
}
string(1) "A"
string(1) "A"

=================
array(1) {
  ["1"]=>
  string(1) "A"
}

Notice: Undefined offset: 1 in /in/bbleq on line 12

Notice: Undefined offset: 1 in /in/bbleq on line 12
NULL
NULL

Patches

Pull Requests

Pull requests:

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2016-05-23 00:59 UTC] yohgaki@php.net
https://3v4l.org/7tshA

PHP 7 behaves like HHVM. PHP 5.6 does not.
 [2016-05-23 01:00 UTC] yohgaki@php.net
-Package: Unknown/Other Function +Package: Scripting Engine problem
 [2016-05-23 01:08 UTC] unirgy at gmail dot com
Just to confirm, are you saying this is an expected behavior and change from 5.6?
 [2016-05-23 01:37 UTC] yohgaki@php.net
IMO. 5.6's behavior is right one.

If $var is array and has 1 as key, it must be accessible as $var[1] and/or $var['1']
 [2016-05-24 10:37 UTC] cmb@php.net
Isn't this basically the same issue as bug #70925?
 [2016-05-25 00:12 UTC] yohgaki@php.net
I'm not sure why JSON does special(?) thing. In general, array with numeric index should be able to be accessed.

https://3v4l.org/VIhtW

Our manual is not correct about object to array cast. It does not have to be inaccessible like above. 

We should avoid inconsistent behavior. It seems some overhead is required fix this inconsistency. Question would be "is it too severe or not?".
 [2016-06-02 12:32 UTC] cmb@php.net
> Our manual is not correct about object to array cast. It does
> not have to be inaccessible like above.

When casting from array to object, and casting back to array,
numeric keys are indeed accessible. However, constructing an
object with numeric keys, and casting to array, makes numeric keys
inaccessible, see <https://3v4l.org/Gsjtv>.

While this is somewhat inconsistent for itself, json's behavior is
consistent with this inconsistent behavior.
 [2016-11-14 18:33 UTC] ajf@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: ajf
 [2016-11-14 18:33 UTC] ajf@php.net
The fix for this bug has been committed.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
http://snaps.php.net/.

 For Windows:

http://windows.php.net/snapshots/
 
Thank you for the report, and for helping us make PHP better.

Fixed by: https://wiki.php.net/rfc/convert_numeric_keys_in_object_array_casts
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon Oct 14 06:01:27 2024 UTC