I need to convert this class array
$categoriesArray = array();
// ID, PARENT_ID, NAME
$categoriesArray[] = Category::create(1, 0, "Category A");
$categoriesArray[] = Category::create(2, 0, "Category B");
$categoriesArray[] = Category::create(3, 0, "Category C");
$categoriesArray[] = Category::create(4, 2, "Sub-cat F");
$categoriesArray[] = Category::create(5, 2, "Sub-Cat G");
$categoriesArray[] = Category::create(6, 3, "Sub-Cat H");
Into this:
$x = new CategoryTree("Main");
$x->add_sub_cat(new CategoryTree($categoriesArray[0]->name)); //Category A
$x->add_sub_cat(new CategoryTree($categoriesArray[1]->name)); //Category B
$x->add_sub_cat(new CategoryTree($categoriesArray[2]->name)); //Category C
$x->subCats[1]->add_sub_cat(new CategoryTree($categoriesArray[3]->name)); //Sub-Cat F
$x->subCats[1]->add_sub_cat(new CategoryTree($categoriesArray[4]->name)); //Sub-Cat G
$x->subCats[2]->add_sub_cat(new CategoryTree($categoriesArray[5]->name)); //Sub-Cat H
subCats can be unlimited hierarchy, so I don't know how many levels there will be. I understand, that recursion is required, but I have no idea how to convert it to that.
I need this because I am doing category tree and I'am trying to make a form, to add categories (by entering name and selecting parent).
If there is any way to add category to something like my second example, it would help me alot.