I am sure you could write that code but for my money AllWebMenus from
Likno makes more sense. You can craft just about any kind of menu and if installed properly will not require you to rebuild it if you add menu items. They have a WordPress plugin to make this "relatively" easy. Be prepared for a little bit of a learning curve but once I went through it and saw how it worked I was quite impressed. You can even build the "Super Menus"