Bug #76186 [Com]: ArrayIterator is always Json encoded as an Object
| From: | fractalesque at gmail dot com | Date: | Thu, 05 Apr 2018 14:14:04 +0000 |
| Subject: | Bug #76186 [Com]: ArrayIterator is always Json encoded as an Object | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-214619@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=76186&edit=1
ID: 76186
Comment by: fractalesque at gmail dot com
Reported by: lionel at lionelmartin dot eu
Summary: ArrayIterator is always Json encoded as an Object
Status: Open
Type: Bug
Package: SPL related
Operating System: Linux
PHP Version: 5.6.35
Block user comment: N
Private report: N
New Comment:
I fail to understand the "bug" part of your issue. It really is expected that an
ArrayObject representation of an array be encoded as an object in json.
The contrary would be quite unsettling, actually.
Previous Comments:
------------------------------------------------------------------------
[2018-04-05 14:01:52] lionel at lionelmartin dot eu
Description:
------------
When using json_encode on an ArrayIterator (or ArrayObject), the result is always an object contrary
to its json_encoded array representation.
Test script:
---------------
$array = ['my', 'test', 'array'];
$arrayObject = new \ArrayObject($array);
$arrayIterator = $arrayObject->getIterator();
echo json_encode($array);
echo json_encode($arrayObject);
echo json_encode($arrayIterator);
Expected result:
----------------
["my","test","array"]
["my","test","array"]
["my","test","array"]
Actual result:
--------------
["my","test","array"]
{"0":"my","1":"test","2":"array"}
{"0":"my","1":"test","2":"array"}
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=76186&edit=1