2

I have this PHP array:

$a = array(
  "french"=>"bonjour",
  "italian"=>"buongiorno",
  "spanish"=>"buenos dias"
);

But when I do echo json_encode($a);, I get:

{
   "french": "bonjour",
   "italian": "buongiorno",
   "spanish": "buenos dias"
}

i.e. a JSON object, but I want a JSON array, at the expense of preserving the string keys, like that :

[
   "bonjour",
   "buongiorno",
   "buenos dias"
]

How should I do it?

11
  • 2
    There is no thing like a "JSON object" or "JSON array". JSON is a text representation of a data structure (usually an object or an array). When it is decoded a data structure similar to the one used to create it is obtained.
    – axiac
    Apr 3, 2018 at 16:49
  • I understand, but I mean I want [ ] brackets and not { }. Apr 3, 2018 at 16:52
  • 1
    @axiac, there are things called objects and arrays in JSON, so your statement is a bit misleading. Care to clarify? Apr 3, 2018 at 16:52
  • What is the exact content of the JSON output that you want, @JacopoStanchi? Also, think about what the according PHP representation would be! Apr 3, 2018 at 16:53
  • What's your expected result?
    – k0pernikus
    Apr 3, 2018 at 16:54

2 Answers 2

4

You can use array_values:

echo json_encode(array_values($a));
0
1

Php arrays are not "arrays" in the strict sense (collection of string/object/numbers/etc, identified through a numeric index), but are associative arrays. Also know as dictionaries or hash maps. They are for key-value storage.

JSON does not support dictionaries as a type, and hence json_encode transforms these to a json object by design, as objects are supported.

Using json_decode, you can determine by the second parameter if you want a hash map (php array) or an object back:

$a = array(
     "french" => "bonjour",
     "italian" => "buongiorno",
     "spanish" => "buenos dias"
);

$json = json_encode($a);

$object = json_decode($json, false); // this is the default behavior
$array = json_decode($json, true);

var_dump($object); // the object
var_dump($array); // same as the starting array

The object will be:

object(stdClass)#1 (3) {
  ["french"]=>
  string(7) "bonjour"
  ["italian"]=>
  string(10) "buongiorno"
  ["spanish"]=>
  string(11) "buenos dias"
}

And the array will be:

array(3) {
  ["french"]=>
  string(7) "bonjour"
  ["italian"]=>
  string(10) "buongiorno"
  ["spanish"]=>
  string(11) "buenos dias"
}
2
  • Hashing is irrelevant, as it's rather an implementation detail. The term you mean is dictionary, I would say. The distinction in PHP is whether the PHP array has zero-based numeric keys (is thus a "normal" array) or not. The former is encoded as JSON array, the latter as JSON object. Apr 3, 2018 at 17:02
  • @UlrichEckhardt The child has different names yet all mean the same thing. My main point is that JSON was never intended to represent php's associative arrays.
    – k0pernikus
    Apr 3, 2018 at 17:08

Not the answer you're looking for? Browse other questions tagged or ask your own question.