php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #76795 json_encode option that outputs associative arrays as arrays instad of objects
Submitted: 2018-08-26 12:56 UTC Modified: 2018-08-29 22:00 UTC
From: kenny dot retzlaff at gmail dot com Assigned:
Status: Suspended Package: JSON related
PHP Version: 7.2.9 OS: *
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 this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: kenny dot retzlaff at gmail dot com
New email:
PHP Version: OS:

 

 [2018-08-26 12:56 UTC] kenny dot retzlaff at gmail dot com
Description:
------------
Introduce an json_encode option that ignores all keys of associative arrays and writes the json object in the [] syntax instead of the {} syntax. Without having to use array_values(). So it can be used in objects that contain multiple arrays.

E.g.
$obj = array("key1" => "value", "key2" => "value2");
json_encode($obj, JSON_FORCE_ARRAY); // ["value", "value2"]



Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2018-08-26 13:11 UTC] peehaa@php.net
javascript does not do "associative arrays" like PHP so what you are asking for is not valid JSON.
 [2018-08-26 13:12 UTC] peehaa@php.net
Actually when reading what you are asking for again it's just:

json_encode(array_values($obj));

So imo no need to introduce yet another constant for something you can already easily do with the existing tooling.
 [2018-08-29 22:00 UTC] cmb@php.net
-Status: Open +Status: Suspended
 [2018-08-29 22:00 UTC] cmb@php.net
In my opinion, a general array_values_recursive() *might* be more
sensible.  Anyhow, this feature clearly needs discussion on the
internals@ mailing list (and maybe even an RFC[1]).  For the time
being, I'm suspending this request.

[1] <https://wiki.php.net/rfc/howto>
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 25 13:01:30 2024 UTC