You should get your data from the Database all at once:
What about doing this:
SELECT menuChapters.id, menuUnits.id
FROM menuChapters INNER JOIN menuUnits
ON (menuChapters.id = menuUnits.chapter_id)
WHERE menuChapters.class=$get_class AND menuChapters.course=$get_course AND menuChapters.visibility=1
ORDER BY menuChapters.chapterOrder ASC, menuUnits.unitsOrder ASC
This should give you a list where you have the id of your chapter and the id of your unit for each row.
I didn't try the code, so it might have some issues.