law Posted May 4, 2009 Share Posted May 4, 2009 According to a comment in the manual this should work: <?php $array = array('apples','bananas','cranberries','durians'); $last_item = end($array); foreach($array as $item) { if ($item == $last_item) { print "I like to eat " . $item; } } ?> source : http://us3.php.net/manual/en/control-structures.foreach.php comment by grobemo here is my code: foreach ($pieces as &$p2){ $last_piece = end($p2); if($p2 = $last_piece && !isset($error)) { $error = "No Error"; } } Here is my error message: "Warning: end() [function.end]: Passed variable is not an array or object in C:\wamp\www\index.php on line 2 (in my example)" Thanks guys I have been posting on the board all day. You guys have been a savior helping me meet this deadline. Quote Link to comment https://forums.phpfreaks.com/topic/156866-solved-finding-last-item-in-a-foreach/ Share on other sites More sharing options...
Ken2k7 Posted May 4, 2009 Share Posted May 4, 2009 Just echo $last_item. Quote Link to comment https://forums.phpfreaks.com/topic/156866-solved-finding-last-item-in-a-foreach/#findComment-826318 Share on other sites More sharing options...
DarkWater Posted May 4, 2009 Share Posted May 4, 2009 Try following the example from php.net. You're calling end() on each individual element in the array, instead of once on the whole array, before the loop. Quote Link to comment https://forums.phpfreaks.com/topic/156866-solved-finding-last-item-in-a-foreach/#findComment-826323 Share on other sites More sharing options...
Ken2k7 Posted May 4, 2009 Share Posted May 4, 2009 Try following the example from php.ner. You're calling end() on each individual element in the array, instead of once on the whole array, before the loop. law did it correctly. He/She just didn't need to loop. Quote Link to comment https://forums.phpfreaks.com/topic/156866-solved-finding-last-item-in-a-foreach/#findComment-826326 Share on other sites More sharing options...
DarkWater Posted May 4, 2009 Share Posted May 4, 2009 Try following the example from php.ner. You're calling end() on each individual element in the array, instead of once on the whole array, before the loop. law did it correctly. He/She just didn't need to loop. foreach ($pieces as &$p2){ $last_piece = end($p2); $pieces is the array, $p2 is the individual element. What's wrong with that picture? Also: if ($p2 = last_piece ... Should be 2 =. Quote Link to comment https://forums.phpfreaks.com/topic/156866-solved-finding-last-item-in-a-foreach/#findComment-826333 Share on other sites More sharing options...
Ken2k7 Posted May 4, 2009 Share Posted May 4, 2009 Ha! I'm stupid. I was looking at the first example. Don't mind me. Sorry all. Quote Link to comment https://forums.phpfreaks.com/topic/156866-solved-finding-last-item-in-a-foreach/#findComment-826336 Share on other sites More sharing options...
law Posted May 5, 2009 Author Share Posted May 5, 2009 Ha! I'm stupid. I was looking at the first example. Don't mind me. Sorry all. UGH! and I just learned that $p2 was the individual elements of the array earlier today! Apparently my brain is on vacation. $last_piece = end($pieces); //echo $last_piece; foreach ($pieces as &$p2){ // lots of unimportant code here if($p2 == $last_piece && !isset($error)) { $error = "No Error"; } } Works like a charm! Thanks again Quote Link to comment https://forums.phpfreaks.com/topic/156866-solved-finding-last-item-in-a-foreach/#findComment-826344 Share on other sites More sharing options...
Ken2k7 Posted May 5, 2009 Share Posted May 5, 2009 That seems redundant. Like I said, why not just skip the for loop and echo $last_piece? Quote Link to comment https://forums.phpfreaks.com/topic/156866-solved-finding-last-item-in-a-foreach/#findComment-826347 Share on other sites More sharing options...
law Posted May 5, 2009 Author Share Posted May 5, 2009 That seems redundant. Like I said, why not just skip the for loop and echo $last_piece? I am trying to identify the last pass through of my foreach loop. I am doing a lot of other things utilizing the loop. I have stripped all of that out as it has nothing to do with this question. I am only using the $last_item to identify the last item in the array. The if() then checks to make sure in my code, that is not included, that there were no identified errors in the loop. As each time the loop runs it does many things which might have resulted in a error due to the way the user inputted the string I am parsing. This whole thing prevents the same error from being printed out to the user more than one time. With out the if() it would show the error every time the error was encountered. Hope that made some sense? I commented where the missing code would have been in my previous post if that helps visualize what I am talking/typing about. Quote Link to comment https://forums.phpfreaks.com/topic/156866-solved-finding-last-item-in-a-foreach/#findComment-826412 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.