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 935055
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T21:05:55+00:00 2026-05-15T21:05:55+00:00

How do I form a multidimensional array using MySQL data? I’m using this code

  • 0

How do I form a multidimensional array using MySQL data? I’m using this code right now:

while($row = mysql_fetch_assoc($result)){
    $music[$row['artist']][$row['title']][$row['id']][$row['category_id']] = $row['key'];
}

And I think it would be much easier to use a multi-dimensional array to store my data (artist array => title array => id, category_id, key) but I have no idea how to form one using data from a database. I’ve googled and can only find examples of static/local data multidimensional arrays, which is no good to me.

I’d also like to know how to output multidimensional data in php for real-world use. Would I use loops? Or would there be an another way?

And I’d just like to say thanks for all the help I’ve gotten so far at stackoverflow, it’s saved me a great deal of headaches!

  • 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-05-15T21:05:56+00:00Added an answer on May 15, 2026 at 9:05 pm
    $music = array();
    while($row = mysql_fetch_assoc($result)){
        if ( !isset($music[$row['artist']] ) {
            $music[$row['artist']] = array()
            }
        $music[$row['artist']][$row['title']]=array('id'=>$row['id'],'category_id'=>$row['category_id'],'key'=>$row['key'];
        }
    

    or more simply if memory requirement is not terribly important (I do this mostly):

    $music = array();
    while($row = mysql_fetch_assoc($result)){
        if ( !isset($music[$row['artist']] ) {
            $music[$row['artist']] = array()
            }
        $music[$row['artist']][$row['title']]=$row;
        }
    

    Using two for-as loops would work — assuming you want to display a tree of artists, than subtrees of titles:

    foreach ( $music as $artist => $titles ) {
        foreach ( $titles as $title => $details ) {
            // do stuff, $details contains all the details for the specific $artist and $title
            }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a multidimensional array converted to JSON data in this format. [[null,null,null,null,null,null],[null,null,null,1,1,null],[null,null,null,null,1,1],[null,null,null,null,null,null],[null,null,null,null,null,null]] I
Found this post that helped me out: Split a string to form multidimensional array
I'm using a multidimensional array in a form and I want to parse these
I have a multidimensional array of form data thats an been unserialized from YAML.
I am POSTing a multidimensional array with the following fields in a HTML form:
For eg I have this code on the main page. <?php session_start(); $_SESSION['order']=array(); ?>
I have an array which is multidimensional for no reason /* This is how
i have a multidimensional array whose index/keys (not the values) are like this: this
I need to form an multidimensional array as below format however I could not
I've been using Serialize() to pass checkbox form data with Post() for a basket

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.