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
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please — but make sure to vote on the bug!
Your email address:
MUST BE VALID
Solve the problem:
18 - 9 = ?
Subscribe to this entry?

 
 [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 Mar 28 13:01:28 2024 UTC