Move the $index counter as static into the anonymous function