Bug #72254 [Opn]: get_object_vars() returns unusable result when keys are numeric
| From: | yohgaki@php.net | Date: | Wed, 25 May 2016 00:12:02 +0000 |
| Subject: | Bug #72254 [Opn]: get_object_vars() returns unusable result when keys are numeric | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-201266@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=72254&edit=1
ID: 72254
Updated by: yohgaki@php.net
Reported by: unirgy at gmail dot com
Summary: get_object_vars() returns unusable result when keys
are numeric
Status: Open
Type: Bug
Package: Scripting Engine problem
Operating System: Any
PHP Version: 7.0.6
Block user comment: N
Private report: N
New Comment:
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?".
Previous Comments:
------------------------------------------------------------------------
[2016-05-24 10:37:57] cmb@php.net
Isn't this basically the same issue as bug #70925?
------------------------------------------------------------------------
[2016-05-23 01:37:52] 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-23 01:08:03] unirgy at gmail dot com
Just to confirm, are you saying this is an expected behavior and change from 5.6?
------------------------------------------------------------------------
[2016-05-23 00:59:20] yohgaki@php.net
https://3v4l.org/7tshA
PHP 7 behaves like HHVM. PHP 5.6 does not.
------------------------------------------------------------------------
[2016-05-23 00:47:09] 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
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=72254&edit=1