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`