Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7756177
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T12:45:17+00:00 2026-06-01T12:45:17+00:00

I currently am pulling menu data out of our database using the PDO fetchAll()

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T12:45:19+00:00Added an answer on June 1, 2026 at 12:45 pm

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am currently working on a project where i am pulling out data using
I am pulling some data from mysql database using PHP, now I would like
Currently I'm using <pre><code> code here </code><pre> to display code. I'm pulling this information
Currently pulling out ma hair over the following problem. My code adds Rectangle s
so im learning php (begginer if you cant tell) and, im currently pushin/pulling data
I am pulling data from my database and trying to encode into JSON data
I'm currently pulling my hair out solving this bug :/ I have already tried
I am currently pulling start times from an XML file and using a date
currently I'm pulling text from a website using public String getText(String uri) { HttpClient
Currently I have an application that takes information from a SQLite database and puts

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.