Torakiki
json_encode it seems that, in the case of only one element with index =
0, it returns an incorrect result.
$array = [];
$array[0] = "Some text"
it should return (in Firefox 90.0.2)
{"0": "Some text"}
and instead returns a string like
["Some text"]
Torakiki:
json_encode it seems that, in the case of only one element with index =
0, it returns an incorrect result.
$array = [];
$array[0] = "Some text"
it should return (in Firefox 90.0.2)
{"0": "Some text"}
Why? $array is still just an array with one scalar element.
and instead returns a string like
["Some text"]
Which is valid json as well. This defines an array with one element.
See: <https://www.json.org/json-en.html>
expresses an array, which (in this case) contains one element.["Some text"]
$array[101] = "Some text 2"
$array[1234] = "Some text 3"
Why? $array is still just an array with one scalar element.
Torakiki
Why? $array is still just an array with one scalar element.
Try this (last PHP 7.4.22 x64):
<?php
$array=[];
$array[0]="Some text";
var_dump($array);
$json=json_encode($array);
var_dump($json);
$array=[];
$array[1]="Some text";
var_dump($array);
$json=json_encode($array);
var_dump($json);
IMHO, seem to be a bug!
On Thu, 05 Aug 2021 18:41:04 +0200, Arno Welzel wrote:
Torakiki:
json_encode it seems that, in the case of only one element with index =
0, it returns an incorrect result.
$array = [];
$array[0] = "Some text"
it should return (in Firefox 90.0.2)
{"0": "Some text"}
Why? $array is still just an array with one scalar element.
and instead returns a string like
["Some text"]
Which is valid json as well. This defines an array with one element.
See: <https://www.json.org/json-en.html>
From that link, the OP's "unexpected" string
The OP changed the sequential array into a sparse array by
assigning additional elements asequentially:
$array[101] = "Some text 2"
$array[1234] = "Some text 3"
Because of the "sparseness" of this object, it can't be
expressed as a JSON array, so json_encode() encoded the
input sparse array as a JSON object.
Why? $array is still just an array with one scalar element.
Try this (last PHP 7.4.22 x64):
<?php
$array=[];
$array[0]="Some text";
var_dump($array);
$json=json_encode($array);
var_dump($json);
$array=[];
$array[1]="Some text";
var_dump($array);
$json=json_encode($array);
var_dump($json);
you get
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 351 |
Nodes: | 16 (2 / 14) |
Uptime: | 28:53:56 |
Calls: | 7,634 |
Files: | 12,796 |
Messages: | 5,688,779 |