Add " and create own json array in php -
i php file, need make own json array.
for($i=1;$i<$arraysize+1;$i++){ $idjson[$i]=$i.":".$birdidarray[$i-1]; } for($i=$arraysize+1 ;$i<$arraysize*2+1; $i++){ $idjson[$i]=$i.":".$rankarray[$i-($arraysize+1)]; }
when use
print(json_encode($idjson));
the output : ["0:3","1:15","2:3","3:14","4:1","5:2","6:2"]
but need output ["0":"3","1":"15","2":"3","3":"14","4":"1","5":2","6":"2"]
when going add " mark
for($i=1;$i<$arraysize+1;$i++){ $idjson[$i]=$i.'"'.":".'"'.$birdidarray[$i-1]; } for($i=$arraysize+1 ;$i<$arraysize*2+1; $i++){ $idjson[$i]=$i.'"'.":".'"'.$rankarray[$i-($arraysize+1)]; }
it prints ["0:3","1\":\"15","2\":\"3","3\":\"14","4\":\"1","5\":\"2","6\":\"2"]
how can avoid printing \ sign?
i'm assuming want json object this:
{"0":"3", ... }
the problem here javascript/json distinguishes between key-value pairs, objects, , numerically indexed lists, arrays, while php uses arrays both these things. json_encode
depends on whether php array continuously numerically indexed array, in case json array, or else, in case json object.
what want force json object continuously numerically indexed array. first question here be: why?! if you're sure want (again, why?!), there's json_force_object
flag in php 5.3+:
echo json_encode(array("3", "15", "3"), json_force_object); // {"0":"3","1":"15","2":"3"}
but i'll again that's pretty pointless. if use regular array ["3","15","3"]
, keys elements implicitly 0
, 1
, 2
. there's no need enforce them object keys.
Comments
Post a Comment