Vy, I needed YUI loading from the header file, therefore I changed your function. I hope that is ok.
My changes are in CVS.
I added a second parameter. If it is set to 1 and an array of YUI names is set, the function returns the loading strings only for those YUI libs which have not already been loaded in a previous empty call.
a) within Moodle only require_js ($lib) is called to collect libraries
b) print_header calls require_js() to output the library links
c) from header.html you call require_js($lib, 1) to add library links not yet set
I commented your original lines out , so you can easily see what I have changed.
One note from www.php.net: If you use array_push() to add one element to the array it's better to use $array = because in that way there is no overhead of calling a function. I changed that part.
I needed to replace your recursive call with a foreach loop. I don't know if the recursive call of require_js is slower than a foreach loop??? Do you know?