[英]Need help turning an associative array into nested JSON using PHP
在過去的一周中,我一直在為此苦苦掙扎,這讓我發瘋,所以如果有人可以幫助我,我將永遠感激不已。
查詢數據庫后,我使用以下方法遍歷數據:
while ($row=mysqli_fetch_assoc($result)) {...
這是行的構造方式:
示例行1:
(
"countryId" => "2",
"countryDescription" => "Canada",
"cityId" => "3",
"cityDescription" => "Montreal",
"restaurantFranchiseId" => "2",
"restaurantFranchiseDescription" => "Kentucky Fried Chicken"
)
示例行2:
(
"countryId" => "2",
"countryDescription" => "Canada",
"cityId" => "3",
"cityDescription" => "Montreal",
"restaurantFranchiseId" => "3",
"restaurantFranchiseDescription" => "Taco Bell"
)
請注意,只有餐廳專營權在上述兩行中有所不同。 國家和城市在兩行中都相同。
我想將行變成一個嵌套的JSON文件,如下所示。 如下所示,每個國家/地區都是唯一的對象。 每個城市都是一個唯一的對象,並且它是相應國家對象的子元素。 然而,餐廳特許經營並不是唯一的,因為它們並不與特定的國家或城市掛鈎。
如何根據我的數據在下面創建如上所述的JSON文件?
謝謝!!!
{
"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"
}]
}]
}]
}
您可以使用以下代碼:
$result = [];
$lastCity = [ "cityId" => null ];
$lastCountry = [ "countryId" => null ];
while ($row=mysqli_fetch_assoc($result)) {
if ($row["countryId"] !== $lastCountry["countryId"]) {
// Country is not the same as in previous row, so create
// a new entry for it in the first level of the result array.
// The city and franchises data will be inserted further down.
$result[] = [
"countryId" => $row["countryId"],
"countryDescription" => $row["countryDescription"],
"cities" => []
];
// Get a reference (`&`) to the new country entry added to `$result`.
// Whatever is later changed in `$lastCountry` will change inside the
// `$result` data structure.
$lastCountry = &$result[count($result)-1];
}
if ($row["cityId"] !== $lastCity["cityId"]) {
// City is not the same as in previous row, so create
// a new entry for it in the second level of `$result`.
// We use the `$lastCountry` "shortcut" to manipulate `$result`.
$lastCountry["cities"][] = [
"cityId" => $row["cityId"],
"cityDescription" => $row["cityDescription"],
"restaurantFranchises" => []
];
// Get a reference (`&`) to the new city entry added to `$result`.
// Whatever is later changed in `$lastCity` will change inside the
// `$result` data structure (and `$lastCountry`).
$lastCity = &$lastCountry["cities"][count($lastCountry["cities"])-1];
}
// Create a new entry for the franchise in the third level of `$result`.
// We use the `$lastCity` "shortcut" to manipulate `$result`.
$lastCity["restaurantFranchises"][] = [
"restaurantFranchiseId" => $row["restaurantFranchiseId"],
"restaurantFranchiseDescription" => $row["restaurantFranchiseDescription"],
];
}
看到它在eval.in上運行。
$lastCity
和$lastCountry
這兩個變量是對$result
數據結構中位置的引用(循環的開頭除外,當它們是偽值時)。 要在$result
數組中獲取此類引用,請使用&
運算符 。 可以在沒有這兩個變量的情況下完成此操作,但是這會使賦值語句變得很長,因為每次$result
數組中的最后一個元素從它的cities
數組中的最后一個元素..引用時,您都需要引用。 。等等。
該算法需要將查詢結果集按國家和城市排序,即城市不應該首先是“ New York”,然后是“ Los Angeles”,再是“ New York”。
此外,假定cityId值是唯一的。 例如,美國的城市不應與加拿大的城市具有相同的cityId 。 如果是這種情況,則上面的代碼應稍作修改。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.