{"id":1616,"date":"2017-03-22T13:31:06","date_gmt":"2017-03-22T08:01:06","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=1616"},"modified":"2017-03-29T09:58:48","modified_gmt":"2017-03-29T04:28:48","slug":"determine-first-last-iteration-foreach-loop","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/determine-first-last-iteration-foreach-loop\/","title":{"rendered":"PHP &#8211; How to determine the first and last iteration in a for each loop"},"content":{"rendered":"<p><label class=\"label label-Warning\">PROBLEM :<\/label><\/p>\n<h4 id=\"the-question-is-simple-we-have-a-for-each-loop-in-our-code\"><span style=\"color: #800000;\">The question is simple. we have a for each loop in our code:<\/span><\/h4>\n[pastacode lang=\u201dphp\u201d manual=\u201dforeach(%24array%20as%20%24element)%20%7B%0A%2F%2Fcode%0A%7D\u201d message=\u201dPhp Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p>In this loop, we want to react differently when we are in first or last iteration.<\/p>\n<p>How to do this?<\/p>\n<p>Php loops for each<\/p>\n<p><label class=\"label label-info\">SOLUTION 1:<\/label><\/p>\n<h4 id=\"use-a-counter\"><span style=\"color: #000000;\">Use a counter:<\/span><\/h4>\n[pastacode lang=\u201dphp\u201d manual=\u201d%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\u201d message=\u201dPhp Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p><label class=\"label label-info\">SOLUTION 2:<\/label><\/p>\n<h4 id=\"another-example\"><span style=\"color: #000000;\">Another example:<\/span><\/h4>\n[pastacode lang=\u201dphp\u201d manual=\u201d%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\u201d message=\u201dPhp Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n[ad type=\u201dbanner\u201d]\n<p><label class=\"label label-info\">SOLUTION 3:<\/label><\/p>\n<h4 id=\"try-this-code\"><span style=\"color: #000000;\">Try this code:<\/span><\/h4>\n[pastacode lang=\u201dphp\u201d manual=\u201d%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\u2026.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\u2019LAST%20ELEMENT!\u2019%3B%0A%7D%0A\u201d message=\u201dPhp Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p><label class=\"label label-info\">SOLUTION 4:<\/label><\/p>\n<h4 id=\"if-your-array-has-unique-array-values-then-determining-the-first-and-last-element-is-trivial\"><span style=\"color: #000000;\">If your array has unique array values, then determining the first and last element is trivial:<\/span><\/h4>\n[pastacode lang=\u201dphp\u201d manual=\u201dforeach(%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\u2019FIRST%20ELEMENT!\u2019%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\u2019LAST%20ELEMENT!\u2019%3B%0A%7D%0A\u201d message=\u201dPhp Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p>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).<\/p>\n[pastacode lang=\u201dphp\u201d manual=\u201dforeach(%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\u2019FIRST%20ELEMENT!\u2019%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\u2019LAST%20ELEMENT!\u2019%3B%0A%7D%0A\u201d message=\u201dPhp Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p>Update: Some people are concerned about performance and\/or modifying the array pointer inside a foreach loop.<\/p>\n<p>For those, you can cache the key value before the loop.<\/p>\n[pastacode lang=\u201dphp\u201d manual=\u201dreset(%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\u2019FIRST%20ELEMENT!\u2019%3B%0A%7D%0A\u201d message=\u201dPhp Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p><label class=\"label label-info\">SOLUTION 5:<\/label><\/p>\n<p>A more simplified version of the above and presuming you\u2019re not using custom indexes\u2026<\/p>\n[pastacode lang=\u201dphp\u201d manual=\u201d%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\u201d message=\u201dPhp Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n[ad type=\u201dbanner\u201d]\n<p><label class=\"label label-info\">SOLUTION 6:<\/label><\/p>\n<p>If you only need just the first element then you may try this code.<\/p>\n[pastacode lang=\u201dphp\u201d manual=\u201d%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\u201d message=\u201dPhp Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n","protected":false},"excerpt":{"rendered":"<p>PROBLEM : The question is simple. we have a for each loop in our code: [pastacode lang=\u201dphp\u201d manual=\u201dforeach(%24array%20as%20%24element)%20%7B%0A%2F%2Fcode%0A%7D\u201d message=\u201dPhp Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/] In this loop, we want to react differently when we are in first or last iteration. How to do this? Php loops for each SOLUTION 1: Use a counter: [pastacode lang=\u201dphp\u201d manual=\u201d%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\u201d message=\u201dPhp [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[25],"tags":[3319,3320,3321,1204,3318,3326,3323,3325,3322,3324],"class_list":["post-1616","post","type-post","status-publish","format-standard","hentry","category-php","tag-find-the-last-element-of-an-array-while-using-a-foreach-loop-in-php","tag-how-do-i-loop-through-or-enumerate-a-javascript-object","tag-how-do-you-remove-an-array-element-in-a-foreach-loop","tag-how-does-php-foreach-actually-work","tag-how-to-find-the-foreach-index","tag-how-to-make-last-uploaded-images-appear-first-in-foreach-loop","tag-iteration-problems-with-foreach-loop","tag-php-effective-way-to-determine-last-loop-in-foreach","tag-php-how-to-skip-last-element-in-foreach-loop","tag-while-loop-runs-only-in-the-first-iteration-of-foreach-loop"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/1616","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=1616"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/1616\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=1616"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=1616"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=1616"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}