从重复值的MySQL结果创建多维嵌套数组(PHP) [英] Creating Multidimensional Nested Array from MySQL Result with Duplicate Values (PHP)
问题描述
我目前正在使用PDO fetchAll()函数从菜单数据库中拉出菜单数据。这样做将查询结果的每一行都放在一个数组中,结构如下:
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)
)
)
)
作为一名n00b程序员,我一直在为我的大脑摆脱最后一天,试图找出如何创建这个新的多维数组。看来我可能需要使用几个嵌套的foreach语句和引用,但是我很难得到任何工作。
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
$array = array() ;
while ($row = PDO::fetchAll()) // Replace with relevant Information
{
$section = array();
$items = array();
$prices = array();
if(!array_key_exists($row['MenuId'], $array))
{
$array[$row['MenuId']] = array();
$array[$row['MenuId']]['MenuName'] = $row['MenuName'] ;
$array[$row['MenuId']]['MenuId'] = $row['MenuId'] ;
$array[$row['MenuId']]['Section'] = array();
}
if(!array_key_exists($row['SectionId'], $array[$row['MenuId']]['Section']))
{
$array[$row['MenuId']]['Section'][$row['SectionId']] = array();
$array[$row['MenuId']]['Section'][$row['SectionId']]['SectionId'] = $row['SectionId'] ;
$array[$row['MenuId']]['Section'][$row['SectionId']]['SubmenuName'] = $row['SubmenuName'] ;
$array[$row['MenuId']]['Section'][$row['SectionId']]['Items'] = array() ;
}
$items['ItemName'] = $row['ItemName'] ;
$items['Description'] = $row['Description'] ;
$prices['PriceId'] = $row['PriceId'] ;
$prices['Price'] = $row['Price'] ;
$items['Prices'] = $prices ;
$section['Items'] = $items ;
$array[$row['MenuId']]['Section'][$row['SectionId']]['Items'][] = $items ;
}
var_dump($array);
只需轻微的更改即可使您想要的
With just minor changes you can make it what you want
这篇关于从重复值的MySQL结果创建多维嵌套数组(PHP)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!