Creating Multidimensional Nested Array from MySQL

2019-04-09 23:49发布

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?

1条回答
2楼-- · 2019-04-10 00:23

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

查看更多
登录 后发表回答