I have two MySQL tables, one for fruits and one for vegetables, and I want to query these tables for their contents and return both sets of results in one large JSON dictionary, keyed with Fruits to return the fruit array and Vegetables to return the vegetables array. It currently doesn't work (returns null).
Here is my current code:
//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);
I can separate each of these things out and just
echo json_encode($fruits);
echo json_encode($vegetables);
but then there are two separate arrays returned. Can anyone shed any light?
I'd like my response to look like this:
[{"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"}]
}]