Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #18759

JSON encode "corrupted"?

From Torakiki <torakiki.sanNIENTE@SPAMgmail.com>
Newsgroups comp.lang.php
Subject JSON encode "corrupted"?
Date 2021-08-05 17:43 +0200
Organization Aioe.org NNTP Server
Message-ID <seh0um$ket$1@gioia.aioe.org> (permalink)

Show all headers | View raw


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

Back to comp.lang.php | Previous | NextNext in thread | Find similar


Thread

JSON encode "corrupted"? Torakiki <torakiki.sanNIENTE@SPAMgmail.com> - 2021-08-05 17:43 +0200
  Re: JSON encode "corrupted"? Arno Welzel <usenet@arnowelzel.de> - 2021-08-05 18:41 +0200
    Re: JSON encode "corrupted"? Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2021-08-05 17:20 +0000
      Re: JSON encode "corrupted"? Arno Welzel <usenet@arnowelzel.de> - 2021-08-08 11:43 +0200
  Re: JSON encode "corrupted"? Torakiki <torakiki.sanNIENTE@SPAMgmail.com> - 2021-08-05 21:25 +0200
    Re: JSON encode "corrupted"? "J.O. Aho" <user@example.net> - 2021-08-05 21:49 +0200
    Re: JSON encode "corrupted"? Arno Welzel <usenet@arnowelzel.de> - 2021-08-08 11:44 +0200

csiph-web