Looping over large numbers of items with array_shift() is expensive. Reverse the array and fetch items from the top of the pile. Credit goes to Martin Langhoff for original mysqli fix.