Fixed for empty arrays

The code threw a notice if the array stayed empty in the while loop. Not
sure if returning the empty array is the intended behaviour, though.