I currently am pulling menu data out of our database using the PDO fetchAll() function. Doing so puts each row of the query results into an array in the following structure:
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.
For parsing reasons, I would like to have the array appear in a nested structure with no duplicate values:
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)
)
)
)
As a n00b programmer, I've been racking my brain for the last day trying to figure out how to create this new, multidimensional array. It seems like I may have to use a couple of nested foreach statements and references, but I've had a hard time getting anything to work.
Does anyone know how I can go about doing so?
This looks like what you are looking for
With just minor changes you can make it what you want