json_encode strange behavior

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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

[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux