我想在類別中對子類別進行分組。子類別可具有任何數量的元素,例如這樣的:多維數組分類和子類別
輸出:
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?
好了,所以我的問題是:
如何選擇類別並將它們放入多維數組中的自己的數組中?
我該如何將子類別放入適當的類別數組中?
最後,我如何打印出一個可以有多個子類別的整個多維數組?