Sie sollten zuerst alle Unterkategorien und Kategorien in einer Abfrage abrufen:
SQL:
SELECT sub_category_id,
category_id,
sub_category_name,
category_name
FROM sub_categories a
INNER JOIN categories b
ON a.category_id=b.category_id
PHP:
$categories = array();
while ($row = mysql_fetch_assoc($result)) {
$cat_name = $row['category_name'];
$sub_cat_id = $row['sub_category_id'];
$sub_cat_name = $row['sub_category_name'];
$categories[$cat_name][$sub_cat_id] = $sub_cat_name;
}
var_dump($categories);