Darkmatter5 Posted January 27, 2009 Share Posted January 27, 2009 How can I run a specific variation of some code only when a while loop gets to the last item of an array? I have an example below of what I'm looking for. The array is $list and contains the following items "1,2,3,4" Now how can I have a loop statement that runs this code on all items, but when it's on the last item will run another code. foreach($list as $lst) { echo "$lst[0], "; <- this is run on all elements except the last element if($list is on last element) { echo "$lst[lastelement]"; } } Quote Link to comment https://forums.phpfreaks.com/topic/142567-run-code-when-a-loop-gets-the-last-item-of-an-array/ Share on other sites More sharing options...
.josh Posted January 27, 2009 Share Posted January 27, 2009 foreach($list as $k => $lst) { echo ($list[$k+1])? "$lst[0], " : "$lst[lastelement]"; } Quote Link to comment https://forums.phpfreaks.com/topic/142567-run-code-when-a-loop-gets-the-last-item-of-an-array/#findComment-747127 Share on other sites More sharing options...
genericnumber1 Posted January 27, 2009 Share Posted January 27, 2009 To avoid any E_NOTICE errors you might want to add isset() to the ternary's conditional. Also note this only works for numerically indexed arrays. <?php foreach($list as $k => $lst) { echo (isset($list[$k+1])) ? "$lst[0], " : "$lst[lastelement]"; } Quote Link to comment https://forums.phpfreaks.com/topic/142567-run-code-when-a-loop-gets-the-last-item-of-an-array/#findComment-747131 Share on other sites More sharing options...
Darkmatter5 Posted January 27, 2009 Author Share Posted January 27, 2009 Cool, but ummm can you explain the code? What does the "$k => $lst" do? Is it getting the number of elements and putting that into $k? Then what does the "?" and ":" do? Is the ":" for a range between $lst[0] and the last element? Do I literally use $lst[lastelement]? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/142567-run-code-when-a-loop-gets-the-last-item-of-an-array/#findComment-747134 Share on other sites More sharing options...
.josh Posted January 27, 2009 Share Posted January 27, 2009 foreach($list as $k => $lst) { echo ($list[$k+1])? "$lst[0], " : end($lst); } Quote Link to comment https://forums.phpfreaks.com/topic/142567-run-code-when-a-loop-gets-the-last-item-of-an-array/#findComment-747136 Share on other sites More sharing options...
cooldude832 Posted January 27, 2009 Share Posted January 27, 2009 Its a shorthand syntax to a if statement does this help? <?php foreach($list as $k => $lst) { if (isset($list[$k+1])}{ echo $lst[0]; } else{ echo $lst['lastelement']; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/142567-run-code-when-a-loop-gets-the-last-item-of-an-array/#findComment-747145 Share on other sites More sharing options...
sasa Posted January 27, 2009 Share Posted January 27, 2009 or <?php $list = array(1,2,3,4,5); $last_element = array_pop($list); foreach ($list as $element) { echo "$element isn't last.<br />\n"; } echo "$last_element is last.<br />\n"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/142567-run-code-when-a-loop-gets-the-last-item-of-an-array/#findComment-747252 Share on other sites More sharing options...
cooldude832 Posted January 27, 2009 Share Posted January 27, 2009 or <?php $list = array(1,2,3,4,5); $last_element = array_pop($list); foreach ($list as $element) { echo "$element isn't last.<br />\n"; } echo "$last_element is last.<br />\n"; ?> That will not do an alternative if its last item which was what was desired I think. Quote Link to comment https://forums.phpfreaks.com/topic/142567-run-code-when-a-loop-gets-the-last-item-of-an-array/#findComment-747512 Share on other sites More sharing options...
kenrbnsn Posted January 27, 2009 Share Posted January 27, 2009 Try something like this: <?php $list = range(1,rand(20,99)); // set up -- get some numbers in an array shuffle($list); // set up -- randomize the array $last_element = $list[count($list) - 1]; foreach ($list as $v) if ($v != $last_element) echo "last element not reached: $v <br>"; else echo "last element reached: $v ... $last_element<br>"; ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/142567-run-code-when-a-loop-gets-the-last-item-of-an-array/#findComment-747578 Share on other sites More sharing options...
genericnumber1 Posted January 27, 2009 Share Posted January 27, 2009 or <?php $list = array(1,2,3,4,5); $last_element = array_pop($list); foreach ($list as $element) { echo "$element isn't last.<br />\n"; } echo "$last_element is last.<br />\n"; ?> That will not do an alternative if its last item which was what was desired I think. Actually it will, and it's fairly quick, especially for large data sets, because it removes per-iteration comparisons. Quote Link to comment https://forums.phpfreaks.com/topic/142567-run-code-when-a-loop-gets-the-last-item-of-an-array/#findComment-748014 Share on other sites More sharing options...
cooldude832 Posted January 28, 2009 Share Posted January 28, 2009 My statement was lets say you have 5 items it will do 1 2 3 4 5 Special Instead of 1 2 3 4 Special Don't know which one was asked for Make sense? Quote Link to comment https://forums.phpfreaks.com/topic/142567-run-code-when-a-loop-gets-the-last-item-of-an-array/#findComment-748735 Share on other sites More sharing options...
genericnumber1 Posted January 28, 2009 Share Posted January 28, 2009 My statement was lets say you have 5 items it will do 1 2 3 4 5 Special Instead of 1 2 3 4 Special Don't know which one was asked for Make sense? Yeah, they're certainly different, but the original post had this next to what you describe as the 1..2..3..4..5 -- "this is run on all elements except the last element" So I assumed he wanted a completely different action for the last action instead of a supplemental one. That comment did contrast some with his initial request, so who knows what he truly wants but him? Quote Link to comment https://forums.phpfreaks.com/topic/142567-run-code-when-a-loop-gets-the-last-item-of-an-array/#findComment-748867 Share on other sites More sharing options...
Mchl Posted January 28, 2009 Share Posted January 28, 2009 I'm just feeling like coding something fancy tonight $arr = range(1,4); //create array of numbers 1,2,3,4 for ($i=1;$i<count($arr);$i++) { // code for all but last elements } //code for last element Not tested. Look crappy... But adds to variation Quote Link to comment https://forums.phpfreaks.com/topic/142567-run-code-when-a-loop-gets-the-last-item-of-an-array/#findComment-748907 Share on other sites More sharing options...
sasa Posted January 28, 2009 Share Posted January 28, 2009 <?php echo 'Code 1:<br />'; $list = array(1,2,3,4); echo implode(', ', $list); echo "<hr />\n"; echo 'Code 2:<br />'; $list = array(1,2,3,4); $pre = ''; foreach ($list as $item){ echo $pre, $item; $pre = ', '; } echo "<hr />\n"; echo 'Code 3:<br />'; $list = array(1,2,3,4); $last = array_pop($list); foreach ($list as $item){ echo $item, ', '; } echo $last; echo "<hr />\n"; echo 'Code 4:<br />'; $list = array(1,2,3,4); $i = 0; while (isset($list[$i +1])){ echo $list[$i++], ', '; } echo $list[$i]; echo "<hr />\n"; //etc. ?> Quote Link to comment https://forums.phpfreaks.com/topic/142567-run-code-when-a-loop-gets-the-last-item-of-an-array/#findComment-748954 Share on other sites More sharing options...
Morg. Posted August 9, 2011 Share Posted August 9, 2011 For SQL query generating scripts, or anything that does a different action for the first or last elements, it is much faster (almost twice as fast) to avoid using unneccessary variable checks. The current accepted solution uses a loop and a check within the loop that will be made every_single_iteration, the correct (fast) way to do this is the following : $numItems = count($arr); $i=0; $firstitem=$arr[0]; $i++; while($i<$numItems-1){ $some_item=$arr[$i]; $i++; } $last_item=$arr[$i]; $i++; A little homemade benchmark showed the following: test1: 100000 runs of model morg time: 1869.3430423737 milliseconds test2: 100000 runs of model if last time: 3235.6359958649 milliseconds And it's thus quite clear that the check costs a lot, and of course it gets even worse the more variable checks you add Quote Link to comment https://forums.phpfreaks.com/topic/142567-run-code-when-a-loop-gets-the-last-item-of-an-array/#findComment-1254866 Share on other sites More sharing options...
Maq Posted August 9, 2011 Share Posted August 9, 2011 While we appreciate your solution, this thread is over 2 1/2 years old, please don't resurrect threads. Quote Link to comment https://forums.phpfreaks.com/topic/142567-run-code-when-a-loop-gets-the-last-item-of-an-array/#findComment-1254895 Share on other sites More sharing options...
Morg. Posted August 10, 2011 Share Posted August 10, 2011 While I totally respect your authority and your forum, this thread is still one of the main google hits on the subject, and should thus be complete in my humble opinion. (I know I'm a mod too and I hate necro .. but this issue is as valid today as it was 2.5 years ago) Quote Link to comment https://forums.phpfreaks.com/topic/142567-run-code-when-a-loop-gets-the-last-item-of-an-array/#findComment-1255168 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.