tags:

views:

66

answers:

1
+1  Q: 

php json encode

This below output come from php json_encode.What we see here is 0849 is twice.Since javascript only use sn to get value why we need the "0" value.The main problem is speed execution. If 800 kb data might can be reduce to 400 kb

{"success":"true","total":968,"data":[{"0":"0849","sn":"0849" }]

If no solution i have to make a script to filter in json_encode so no need twice data transter.

+4  A: 

The 0 entry is because your array contains a 0 entry! I'm going to guess that you're retrieving it from a database, using something like mysql_fetch_array; in this case, you can simply change to using mysql_fetch_assoc (which returns a straight forward associative array), or by passing MYSQL_ASSOC as the second argument to mysql_fetch_array. If you're using other DB sources, similar functions exist.

If it's not coming from a database (or you need the numeric keys elsewhere) you can always filter the numerical values out. You can do this by stringing a bunch of array_* functions together, or just with a foreach loop:

foreach ($data as $k => $v) { if (is_int($k)) { unset($data[$k]); } }
Chris Smith
@Chris Smith : I got the exactly same weird json output... So I guess your answer is right! Thanks for this :)
Michael Mao
yes thanks i solve it.Even though a second different .It's important to me. from 328.4 kb to 158.2 kb.Need to optimize more....
hafizan