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

  • Home
  • SEARCH
  • 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 8306077
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T18:15:30+00:00 2026-06-08T18:15:30+00:00

I have 2 database tables: categories and sub_categories . sub_categories table is linked to

  • 0

I have 2 database tables: categories and sub_categories. sub_categories table is linked to categories table by category id cat_id

In php I want to print the parent category and all the sub-categories belonging to it. Is it possible to do so by creating a multidimensional array from 1 mysql query?

Example result:

array
(
  "Category 1"=>array
  (
  "Sub-category",
  "Sub-category",
  "Sub-category"
  ),
  "Category 2"=>array
  (
  "Sub-category"
  ),
  "Category 3"=>array
  (
  "Sub-category",
  "Sub-category",
  "Sub-category"
  )
 );

My query returns just 1 sub category for each category:

SELECT `categories`.`cat_title`, `sub_categories`.`sub_cat_title` 
FROM (`categories`) 
LEFT JOIN `sub_categories` 
ON `sub_categories`.`cat_id` = `categories`.`cat_id` 
GROUP BY `categories`.`cat_title`
  • 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-08T18:15:31+00:00Added an answer on June 8, 2026 at 6:15 pm

    MySQL wont return an array, it will only return rows, but you can easily create the array with php.. just do this

    <?php
      $db = new mysqli('host','user','password','database');
      $query =  "SELECT c.cat_title, s.sub_cat_title
                 FROM categories c
                 LEFT JOIN sub_categories s USING (cat_id)
                 ORDER BY cat_title";
      $result = $db->query($query);
      while($row = $result->fetch_assoc()){
        $cats[$row['cat_title']][] = $row['sub_cat_title'];
      }        
    
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a database table named Categories . I want to create a form
I have Product and Category tables in database. One product can have many categories.
I have 2 database tables named Categories and SubCategories and I want to create
Let's say I have two tables in my database. TABLE:Categories ID|CategoryName 01|CategoryA 02|CategoryB 03|CategoryC
I have an existing database that contains 4 tables: categories, sub-categories, products, prices. I
Do they have a table for all categories and another for all sub-categories (and
I have a database with the following tables: create table Categories ( Id int
I have two database tables, 1st is the TABLE_GLOBAL_PRODUCTS where all products information are
I have 3 database tables, all of them have the same 5 columns. They
I have three database tables: users emails invitations Emails are linked to users by

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.