json_encode strange behavior

From: Date: Fri, 31 May 2013 15:16:19 +0000
Subject: json_encode strange behavior
Groups: php.general 
Request: Send a blank email to php-general+get-321301@lists.php.net to get a copy of this message
Hi. I'm encoding some data with json_encode to use with jquery. All working fine, postgres query, my jquery, etc. But when I cast json_encode in my array, the result is an json object ordered by the array index, in my case the id of my clients. This is right? Is the intended behavior? Anyway, I fixed by putting quotes around the array key. Code that order by array key: $lista = new sig; $r = $lista->doQuery("SELECT cliente_id, razao_social FROM clientes.clientes ORDER BY razao_social ASC"); while ( ($o = pg_fetch_object($r)) ) { $niveis[$o->cliente_id] = $o->razao_social; } echo json_encode($niveis); Postgres result: cliente_id - razao_social 10 - Client A 5 - Client B 1 - Client C json_encode output: {1:"Client C",5:"Client B",10:"Client A"} This works fine (note the quotes around the array index): $lista = new sig; $r = $lista->doQuery("SELECT cliente_id, razao_social FROM clientes.clientes ORDER BY razao_social ASC"); while ( ($o = pg_fetch_object($r)) ) { $niveis["".$o->cliente_id.""] = $o->razao_social; } echo json_encode($niveis); Postgres result: cliente_id - razao_social 10 - Client A 5 - Client B 1 - Client C json_encode output: {"10":"Client A","5":"Client B","1":"Client C"} Thanks! -- ----- Bruno Hass (51) 8911-9274

« previous php.general (#321301) next »