• JSON encode "corrupted"?

    From Torakiki@21:1/5 to All on Thu Aug 5 17:43:18 2021
    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"]

    If I add other elements, like

    $array[101] = "Some text 2"
    $array[1234] = "Some text 3"

    the result is correct:

    {"0":"Some text","101":"Some text 2","1234":"Some text 3"}

    Try this code:

    <?php

    $array=[];
    $array[0]="Some text";
    var_dump($array);

    $json=json_encode($array);
    var_dump($json);


    $array=[];
    $array[0]="Some text";
    $array[101]="Some text 2";
    $array[1234]="Some text 3";
    var_dump($array);

    $json=json_encode($array);
    var_dump($json);



    Thank's

    --
    Torakiki

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Arno Welzel@21:1/5 to All on Thu Aug 5 18:41:04 2021
    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>


    --
    Arno Welzel
    https://arnowelzel.de

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Lew Pitcher@21:1/5 to Arno Welzel on Thu Aug 5 17:20:08 2021
    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
    ["Some text"]
    expresses an array, which (in this case) contains one element.

    Had the OP added
    $array[1] = "Other text"
    the results probably would have been expressed as
    ["Some text","Other text"]

    But...

    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.

    --
    Lew Pitcher
    "In Skills, We Trust"

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Torakiki@21:1/5 to All on Thu Aug 5 21:25:25 2021
    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

    ---------------------------------------------
    array(1) {


    [0]=>



    string(9) "Some text"


    }



    string(13) "["Some text"]"




    array(1) {


    [1]=>



    string(9) "Some text"


    }



    string(17) "{"1":"Some text"}" ---------------------------------------------

    IMHO, seem to be a bug!

    --
    Torakiki

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From J.O. Aho@21:1/5 to Torakiki on Thu Aug 5 21:49:48 2021
    On 05/08/2021 21:25, Torakiki wrote:

    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!

    nope, not a bug, just a standard how to represent different types of arrays. https://cswr.github.io/JsonSchema/spec/arrays/

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Arno Welzel@21:1/5 to All on Sun Aug 8 11:43:11 2021
    Lew Pitcher:

    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

    Why? The string

    ["some text"]

    is a valid JSON array.

    Also see here:

    <https://jsonformatter.curiousconcept.com>
    <http://json.parser.online.fr>

    [...]
    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.

    Correct.


    --
    Arno Welzel
    https://arnowelzel.de

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Arno Welzel@21:1/5 to All on Sun Aug 8 11:44:50 2021
    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);



    you get

    Can you PLEASE format your output to NOT take up so much space, thank you!

    And no this is not a bug, since JSON arrays can't have keys and therefor
    PHP MUST create a JSON object list instead of an array.



    --
    Arno Welzel
    https://arnowelzel.de

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)