How to determine the first and last iteration in a foreach loop? If your array has unique array values, then determining the first and last element is trivial: