我想在类别中对子类别进行分组。子类别可具有任何数量的元素,例如这样的:多维数组分类和子类别
输出:
category #1
item 1
item 2
item 3
category #2
item 1
item 2
item 3
item 4
item 5
我最初的计划是使用一个多维数组这样的:
$categories = array(
'cateogy_name_1' => array(
1 => 'item_1',
2 => 'item_2',
...
),
'cateogy_name_2' => array(
1 => 'item_1',
2 => 'item_2',
3 => 'item_3',
4 => 'item_4',
5 => 'item_5',
...
),
....
);
我迄今代码.. 。
$categories = array();
$result= mysql_query("SELECT category_id, product_name FROM `table` GROUP BY
`catagory_id` ORDER BY `catagory_id`"); //retreive all categories and sub-categories
while($row = mysql_fetch_array($result))
{
//Get Categories and create arrays inside a single array
//I'm stuck here, not sure how to initialize the multi-dimensional array
}
foreach // Put all subcategories within the correct categories
// I'm stuck here. How would I get the subcategories and put
// them into the correct category?
好了,所以我的问题是:
如何选择类别并将它们放入多维数组中的自己的数组中?
我该如何将子类别放入适当的类别数组中?
最后,我如何打印出一个可以有多个子类别的整个多维数组?