|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #81112 Special json_encode behavior for SplFixedArray
Submitted: 2021-06-06 20:04 UTC Modified: 2021-06-07 12:51 UTC
From: parsonswy at gmail dot com Assigned:
Status: Closed Package: SPL related
PHP Version: 8.0.7 OS: Ubuntu 20.04.2
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.
Block user comment
Status: Assign to:
Bug Type:
From: parsonswy at gmail dot com
New email:
PHP Version: OS:


 [2021-06-06 20:04 UTC] parsonswy at gmail dot com
SplFixedArray is a class and it's objects are therefore serialized as objects ('{}') by json_encode(), not arrays ('[]'). I've marked this as a feature change because I believe this is technically what is supposed to happen since SplFixedArrays are objects, but I also think this current behavior is not intuitive or useful. SplFixedArray only accepts numeric indices and should always be representable as a JSON array. This would be consistent with the behavior of the primitive array type which will serialize as an array so long as there are no non-integer keys.

My current use case is developing a RESTful API which is queried by Javascript clients. It is annoying to either cast SplFixedArray objects to (array)s before serializing them, or convert them client side. Index access is the same in Javascript whether the key is '0' or the Integer 0, but having a map instead of an array means functions like do not work.

I couldn't find any discussion in an existing bug ticket where a decision about this special case had been made. This ticket is similar, but I think SplFixedArrays warrant a separate decision as ArrayObjects support Map-like access with non-numeric keys and SplFixedArrays do not.

Test script:
$spl_array = new SplFixedArray(1);
$std_array = [];

echo json_encode($spl_array) . PHP_EOL;
echo json_encode( (array) $spl_array ) . PHP_EOL;

echo json_encode($std_array);
$std_array[] = ['0 value'];
echo json_encode($std_array) . PHP_EOL;

Expected result:
[null]  // SplFixedArray intuitive behavior
[null]  // Explicit array cast work around to achieve intuitive behavior

// normal array behavior, when representable as array

Actual result:
{"0":null}  // SplFixedArray current behavior
[null]  // Explicit array cast work around to achieve intuitive behavior

// normal array behavior, when representable as array


Add a Patch

Pull Requests

Pull requests:

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2021-06-07 12:51 UTC]
Adding a JsonSerializable implementation to SplFixedArray would make sense to me.
 [2021-06-08 13:33 UTC]
The following pull request has been associated:

Patch Name: Implement JsonSerializable for SplFixedArray
On GitHub:
 [2021-06-14 08:08 UTC]
Automatic comment on behalf of nikic
Log: Fix bug #81112: Implement JsonSerializable for SplFixedArray
 [2021-06-14 08:08 UTC]
-Status: Open +Status: Closed
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Wed Dec 01 00:03:34 2021 UTC