我目前正在使用 PDO fetchAll() 函数从我们的数据库中提取菜单数据。这样做会将查询结果的每一行放入以下结构的数组中:
Array (
[0] => Array (
[MenuId] => mmnlinlm08l6r7e8ju53n1f58
[MenuName] => Main Menu
[SectionId] => eq44ip4y7qqexzqd7kjsdwh5p
[SubmenuName] => Salads & Appetizers
[ItemName] => Tomato Salad
[Description] => Cucumbers, peppers, scallions and cured tuna
[Price] => $7.00)
[1] => Array (
[MenuId] => mmnlinlm08l6r7e8ju53n1f58
[MenuName] => Main Menu
[SectionId] => xlkadsj92laada9082lkas
[SubmenuName] => Entrees
[ItemName] => Portabello Carpaccio
[Description] => Dried tomatoes, pin nuts, mahon cheese
[Prices] => $18.00)
)
...etc.
出于解析原因,我想让数组出现在没有重复值的嵌套结构中:
Array (
[MenuName] => Main Menu
[MenuId] => mmnlinlm08l6r7e8ju53n1f58
[Section] => Array (
[SectionId] => eq44ip4y7qqexzqd7kjsdwh5p
[SubmenuName] => Salads & Appetizers
[Items] => Array (
[ItemName] => Tomato Salad
[Description] => Cucumbers, peppers, scallions and cured tuna
[Prices] => Array (
[PriceId] => xksjslkajiak1
[Price] => $7.00)
)
[SectionId] => xlkadsj92laada9082lkas
[SubmenuName] => Entrees
[Items] => Array (
[ItemName] => Portabello Carpaccio
[Description] => Dried tomatoes, pin nuts, mahon cheese
[Prices] => Array (
[PriceId => alkadh29189s09
[Price] = $8.00)
)
)
)
作为一名 n00b 程序员,我在最后一天一直在绞尽脑汁想弄清楚如何创建这个新的多维数组。似乎我可能不得不使用几个嵌套的 foreach 语句和引用,但我很难让任何东西工作。
有谁知道我该怎么做?