php loop refactor