php mysql json document makeup -
i'm using of code generate json data form mysql table :
$sql = "select * u3d15"; $result = mysql_query($sql); $json = array(); if(mysql_num_rows($result)){ while($row=mysql_fetch_row($result)){ $json['title'][]=$row; } } mysql_close($db_name); echo json_encode($json);
but json looks like: {"title":[["1","kevin","t0e0gaa"]]}
i be:
{ "title" : "decode json", "id" : 20, "buttons" : [ { "title" : "kevin ", "image" : "t0e0gaa" }, { "title" : "lora ", "image" : "v1awyqr" } ] }
how can change makeup of file?
setup array formatted results.
$json = array(); //create empty array $json["title"] = "decode json"; $json["id"] = 20; $json["buttons"] = array(); //empty nested array - fill mysql results $sql = "select * u3d15"; $result = mysql_query($sql); if(mysql_num_rows($result)){ while($row=mysql_fetch_row($result)){ $json["buttons"][]=$row; //insert mysql rows buttons array } } mysql_close($db_name); echo json_encode($json);
Comments
Post a Comment