我有两个MySQL表,一个用于水果,一个用于蔬菜,我想查询这些表的内容,并在一个大的JSON字典中返回两组结果,用Fruits键控返回水果数组和Vegetables返回蔬菜阵列。它目前不起作用(返回null)。
这是我目前的代码:
//query each table for every object
$fruitResult = mysql_query("SELECT * FROM Fruits");
$vegResult = mysql_query("SELECT * FROM Vegetables");
//set up an array for each kind of object
$fruits = array();
$vegetables = array();
while ($fruitRow = mysql_fetch_assoc($fruitResult)) {
//add each result from Fruit table to array
$fruits[] = $fruitRow;
}
while ($vegRow = mysql_fetch_assoc($vegResult)) {
//add each result from Veggie table to array
$vegetables[] = $vegRow;
}
//create dictionary
$dictionary = array("Fruit"=>$fruits,"Vegetables"=>$vegetables);
echo json_encode($dictionary);
我可以将这些事情分开,只是
echo json_encode($fruits);
echo json_encode($vegetables);
然后返回两个单独的数组。任何人都可以放任何光吗? 我希望我的回答看起来像这样:
[{"Fruit":
[{"id":"0","name":"apple","color":"red"},
{"id":"1","name":"strawberry","color":"red"},
{"id":"2","name":"grape","color":"purple"}],
{"Vegetables":
[{"id":"0","name":"pea","color":"green"},
{"id":"1","name":"asparagus","color":"green"},
{"id":"2","name":"corn","color":"yellow"}]
}]