I've been struggling with this for the past week and it's driving me crazy so if anyone could help me out I'd be forever grateful.
After querying my database I iterate through the data with:
while ($row=mysqli_fetch_assoc($result)) {...
This is how the rows are constructed:
Example row 1:
(
"countryId" => "2",
"countryDescription" => "Canada",
"cityId" => "3",
"cityDescription" => "Montreal",
"restaurantFranchiseId" => "2",
"restaurantFranchiseDescription" => "Kentucky Fried Chicken"
)
Example row 2:
(
"countryId" => "2",
"countryDescription" => "Canada",
"cityId" => "3",
"cityDescription" => "Montreal",
"restaurantFranchiseId" => "3",
"restaurantFranchiseDescription" => "Taco Bell"
)
Notice that only the restaurant franchise differs in the two rows above. The country and the city are the same in both rows.
I want to turn the rows in to a nested JSON-file like the one below. As you can see below, each country is a unique object. Each city is an unique object and a child element of it's corresponding country object. The restaurant franchises however aren't unique, since they aren't tied to a specific country or city.
How can create the JSON-file below from my data, which is structured as described above?
THANKS!!!
{
"Countries": [{
"countryId": "1",
"countryDescription": "USA",
"cities": [{
"cityId": "1",
"cityDescription": "Houston",
"restaurantFranchises": [{
"restaurantFranchiseId": "1",
"restaurantFranchiseDescription": "Mc Donald's"
}, {
"restaurantFranchiseId": "2",
"restaurantFranchiseDescription": "Kentucky Fried Chicken"
}, {
"restaurantFranchiseId": "4",
"restaurantFranchiseDescription": "Pizza Hut"
}]
}, {
"cityId": "2",
"cityDescription": "New york",
"restaurantFranchises": [{
"restaurantFranchiseId": "1",
"restaurantFranchiseDescription": "Mc Donald's"
}, {
"restaurantFranchiseId": "4",
"restaurantFranchiseDescription": "Pizza Hut"
}]
}]
}, {
"countryId": "2",
"countryDescription": "Canada",
"cities": [{
"cityId": "3",
"cityDescription": "Montreal",
"restaurantFranchises": [{
"restaurantFranchiseId": "1",
"restaurantFranchiseDescription": "Mc Donald's"
}, {
"restaurantFranchiseId": "3",
"restaurantFranchiseDescription": "Taco Bell"
}, {
"restaurantFranchiseId": "4",
"restaurantFranchiseDescription": "Pizza Hut"
}]
}, {
"cityId": "4",
"cityDescription": "Ottawa",
"restaurantFranchises": [{
"restaurantFranchiseId": "2",
"restaurantFranchiseDescription": "Kentucky Fried Chicken"
}, {
"restaurantFranchiseId": "3",
"restaurantFranchiseDescription": "Taco Bell"
}, {
"restaurantFranchiseId": "4",
"restaurantFranchiseDescription": "Pizza Hut"
}]
}]
}]
}