Bug #67640 [Com]: Object to array cast results in duck typing
| From: | php at kingsquare dot nl | Date: | Wed, 23 Jul 2014 09:14:41 +0000 |
| Subject: | Bug #67640 [Com]: Object to array cast results in duck typing | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-186779@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=67640&edit=1
ID: 67640
Comment by: php at kingsquare dot nl
Reported by: php at kingsquare dot nl
Summary: Object to array cast results in duck typing
Status: Open
Type: Bug
Package: Scripting Engine problem
Operating System: n/a
PHP Version: 5.5.14
Block user comment: N
Private report: N
New Comment:
True, and the point isn't that the key should be a string or an integer but that it isn't
found at all :)
$json = '{ "0" : "test1" }';
$obj = json_decode($json);
// recast it into an array
$array = (array) $obj;
$array[0] == "test1"
// throws error that the index can't be found but is shown in a var_dump
Previous Comments:
------------------------------------------------------------------------
[2014-07-19 17:08:56] mightyuhu@php.net
The manual states:
A key may be either an integer or a string. If a key is the standard representation of an integer,
it will be interpreted as such (i.e. "8" will be interpreted as 8, while "08"
will be interpreted as "08").
http://php.net/manual/en/language.types.array.php
So according to this i expect
$json = '{ 0 => "test" }';
------------------------------------------------------------------------
[2014-07-18 11:29:15] php at kingsquare dot nl
I'd expect it to be a string due to the json data itself:
$json = '{ "0" => "test" }';
and not:
$json = '{ 0 => "test" }';
In either case: fetching the array using the index would juggle it into int anyway (thus $array[0]
== $array["0"] == "test")
------------------------------------------------------------------------
[2014-07-18 11:04:20] mightyuhu@php.net
In this case i'd expect $array to look like
array(1) { [0]=> string(5) "test1" }
instead of
array(1) { ["0"]=> string(5) "test1" }
test code:
<?php
$json = '{ "0" : "test1" }';
// just decode that into an object
$obj = json_decode($json);
// recast it into an array
$array = get_object_vars($obj);
// just dump to see the results
var_dump($obj,$array, $array[0], $array['0']);
//object(stdClass)#1 (1) { ["0"]=> string(5) "test1" } array(1) { [0]=>
string(5) "test1" } string(5) "test1" string(5) "test1"
$array2 = (array) $obj;
var_dump($obj,$array2, $array2[0], $array2['0']);
//E_NOTICE : type 8 -- Undefined offset: 0 -- at line 10
E_NOTICE : type 8 -- Undefined index: 0 -- at line 10
$array3 = json_decode($json,true);
var_dump($array3, $array3[0], $array3['0']);
//object(stdClass)#1 (1) { ["0"]=> string(5) "test1" } array(1) {
["0"]=> string(5) "test1" } NULL NULL array(1) { [0]=> string(5)
"test1" } string(5) "test1" string(5) "test1"
?>
------------------------------------------------------------------------
[2014-07-17 10:12:55] php at kingsquare dot nl
Description:
------------
Creating objects from (i.e.) a JSON string and casting that to an array results in something that
looks like an array, acts like an array, but in reality is still an object? (or atleast suffers from
not supporting numeric properties on objects)
I know objects can't have numeric properties but dumping the object _and_ array works as
intended... then accessing the array by index 0 it fails? (but accessing the object property like
$obj->{"0"} works, thus i'd expect $array[0] / $array['0'] to work as
well ?
(see reports: 45346 / 61655 perhaps?)
I know about the lookup / juggling mismatch (the index is actually a string "0" but access
via index is juggled into an int and thus cant be found...) Although i understand the problem it has
some issues:
first and foremost: i expected it to work ;) (since creating an $foo =
array("1"=>"test") will work with $foo[1], so from a user perspective there
should be no difference
- this specific use case seems undocumented ?
- If var_dump-ing the resulting array i should be able to either use array_key_exists or by
derefencing to access the key ... If not: var_dump should not show it / mention it.. ;)
Test script:
---------------
$json = '{ "0" : "test1" }';
// just decode that into an object
$obj = json_decode($json);
// recast it into an array
$array = (array) $obj;
// just dump to see the results
var_dump($obj, $array, $array['0'], $array[0]);
Actual result:
--------------
Notices on the undefined index: 0
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=67640&edit=1