MySql
_fetch_assocを使用してからjson_encode
$type = mysql_query("SELECT type_food,category_food,name_food FROM menu ORDER BY type_food,category_food,name_food");
$rows = array();
while($t = mysql_fetch_assoc($type)) {
$rows[]= $t;
}
print json_encode($rows);
これにより、次の結果が得られます。
{"type_food":"pizza","category_food":"Gourmet","name_food":"pepperoni"},
{"type_food":"pizza","category_food":"Gourmet","name_food":"supreme"},
{"type_food":"pizza","category_food":"Gourmet","name_food":"hawaiian"},
{"type_food":"pizza","category_food":"Gourmet","name_food":"tropical"},
{"type_food":"pizza","category_food":"traditional","name_food":"margherita"},
{"type_food":"pizza","category_food":"traditional","name_food":"vegetarian"}
ただし、次のような多次元のjson結果を作成する必要があります。
{"pizza": [
{"Gourmet": [
{"pepperoni"},
{"supreme"},
{"hawaiian"},
{"tropical"}]},
{"traditional": [
{"margherita"},
{"vegetarian"}]}
]},
{"etc": [ ... ]}
これはMySQL連想配列を使用して可能ですか?