Improvements to Hash::expand and Hash::merge. (master)

Because of the recursion in these functions, processing very large
arrays would take a very long time. I rewrote the functions to
eliminate any unnecessary recursion and function calls. Large arrays
are now processed much faster.
We had a request with a big database query where this line was being run 180,000 times. That request took 30sec. After the improvements, it now takes 5sec. :D