The question is simple. we have a for each loop in our code:

[pastacode lang=”php” manual=”foreach(%24array%20as%20%24element)%20%7B%0A%2F%2Fcode%0A%7D” message=”Php Code” highlight=”” provider=”manual”/]

In this loop, we want to react differently when we are in first or last iteration.

How to do this?

Php loops for each

Use a counter:

[pastacode lang=”php” manual=”%24i%20%3D%200%3B%0A%24len%20%3D%20count(%24array)%3B%0Aforeach%20(%24array%20as%20%24item)%20%7B%0A%20%20%20%20if%20(%24i%20%3D%3D%200)%20%7B%0A%20%20%20%20%20%20%20%20%2F%2F%20first%0A%20%20%20%20%7D%20else%20if%20(%24i%20%3D%3D%20%24len%20-%201)%20%7B%0A%20%20%20%20%20%20%20%20%2F%2F%20last%0A%20%20%20%20%7D%0A%20%20%20%20%2F%2F%20%E2%80%A6%0A%20%20%20%20%24i%2B%2B%3B%0A%7D%0A” message=”Php Code” highlight=”” provider=”manual”/]

Another example:

[pastacode lang=”php” manual=”%24arr%20%3D%20array(1%2C%202%2C%203%2C%204%2C%205%2C%206%2C%207%2C%208%2C%209%2C%2010)%3B%0A%0Aforeach%20(%24arr%20as%20%24a)%20%0A%7B%0A%0A%2F%2F%20This%20is%20the%20line%20that%20does%20the%20checking%0Aif%20(!each(%24arr))%20echo%20%22End!%5Cn%22%3B%0A%0Aecho%20%24a.%22%5Cn%22%3B%0A%0A%7D%0A” message=”Php Code” highlight=”” provider=”manual”/] [ad type=”banner”]

Try this code:

[pastacode lang=”php” manual=”%2F%2FStore%20the%20last%20key%0A%24lastkey%20%3D%20key(end(%24array))%3B%20%0Aforeach(%24array%20as%20%24key%20%3D%3E%20%24element)%20%0A%7B%0A%20%20%20%20….do%20array%20stuff%0A%20%20%20%20if%20(%24lastkey%20%3D%3D%3D%20key(%24array))%0A%20%20%20%20%20%20%20%20echo%20’LAST%20ELEMENT!’%3B%0A%7D%0A” message=”Php Code” highlight=”” provider=”manual”/]

If your array has unique array values, then determining the first and last element is trivial:

[pastacode lang=”php” manual=”foreach(%24array%20as%20%24element)%20%7B%0A%20%20%20%20if%20(%24element%20%3D%3D%3D%20reset(%24array))%0A%20%20%20%20%20%20%20%20echo%20’FIRST%20ELEMENT!’%3B%0A%0A%20%20%20%20if%20(%24element%20%3D%3D%3D%20end(%24array))%0A%20%20%20%20%20%20%20%20echo%20’LAST%20ELEMENT!’%3B%0A%7D%0A” message=”Php Code” highlight=”” provider=”manual”/]

This works if last and first elements are appearing just once in an array, otherwise you get false positives. Therefore, you have to compare the keys (they are unique for sure).

[pastacode lang=”php” manual=”foreach(%24array%20as%20%24key%20%3D%3E%20%24element)%20%0A%7B%0A%20%20%20%20reset(%24array)%3B%0A%20%20%20%20if%20(%24key%20%3D%3D%3D%20key(%24array))%0A%20%20%20%20%20%20%20%20echo%20’FIRST%20ELEMENT!’%3B%0A%0A%20%20%20%20end(%24array)%3B%0A%20%20%20%20if%20(%24key%20%3D%3D%3D%20key(%24array))%0A%20%20%20%20%20%20%20%20echo%20’LAST%20ELEMENT!’%3B%0A%7D%0A” message=”Php Code” highlight=”” provider=”manual”/]

Update: Some people are concerned about performance and/or modifying the array pointer inside a foreach loop.

For those, you can cache the key value before the loop.

[pastacode lang=”php” manual=”reset(%24array)%3B%0A%24first%20%3D%20key(%24array)%3B%0Aforeach(%24array%20as%20%24key%20%3D%3E%20%24element)%20%0A%7B%0A%20%20%20%20if%20(%24key%20%3D%3D%3D%20%24first)%0A%20%20%20%20%20%20%20%20echo%20’FIRST%20ELEMENT!’%3B%0A%7D%0A” message=”Php Code” highlight=”” provider=”manual”/]

A more simplified version of the above and presuming you’re not using custom indexes…

[pastacode lang=”php” manual=”%0A%0A%24len%20%3D%20count(%24array)%3B%0Aforeach%20(%24array%20as%20%24index%20%3D%3E%20%24item)%20%7B%0A%20%20%20%20if%20(%24index%20%3D%3D%200)%20%7B%0A%20%20%20%20%20%20%20%20%2F%2F%20first%0A%20%20%20%20%7D%20else%20if%20(%24index%20%3D%3D%20%24len%20-%201)%20%7B%0A%20%20%20%20%20%20%20%20%2F%2F%20last%0A%20%20%20%20%7D%0A%7D%0A” message=”Php Code” highlight=”” provider=”manual”/] [ad type=”banner”]

If you only need just the first element then you may try this code.

[pastacode lang=”php” manual=”%24firstElement%20%3D%20true%3B%0A%0Aforeach%20(%24reportData-%3Eresult()%20as%20%24row)%20%0A%7B%0A%20%20%20%20%20%20%20if(%24firstElement)%20%7B%20echo%20%22first%20element%22%3B%20%24firstElement%3Dfalse%3B%20%7D%0A%20%20%20%20%20%20%20%2F%2F%20Other%20lines%20of%20codes%20here%0A%7D%0A” message=”Php Code” highlight=”” provider=”manual”/]

Categorized in: