nodirtyrockstar Posted October 31, 2012 Share Posted October 31, 2012 I am trying to create a list with array elements. It should add a comma after every element except for the last. I have the following code: foreach ($srch as $value) { $query .= "`" . $value . "`"; if (next($srch)){ $query .= ","; } } It is adding a comma after everything except for the LAST TWO elements. Can someone help me understand this? I have read the php.net manual about next() and it seems like it should work for this purpose. Also, I realize there are other ways to do this, but right now I would really like to understand next(). Link to comment https://forums.phpfreaks.com/topic/270097-foreach-all-but-last-element/ Share on other sites More sharing options...
Pikachu2000 Posted October 31, 2012 Share Posted October 31, 2012 What's in the array? Link to comment https://forums.phpfreaks.com/topic/270097-foreach-all-but-last-element/#findComment-1388877 Share on other sites More sharing options...
kicken Posted October 31, 2012 Share Posted October 31, 2012 foreach does odd things with the internal array pointer. As such you shouldn't really mess with it when your looping your array. Either put your strings in their own array and implode() it or use trim() to remove the last comma. To be somewhat more precise about what happens to the pointer, foreach() resets the pointer to the beginning of the array before it executes, and at some point the pointer gets advanced once more prior to running your first iteration of the loop. So on your first iteration the pointer is at element [1] which means if you call next() each iteration you're going to hit the end of the array in your second to last iteration, not the last one. Note this behavior of what foreach() does with the pointer is not something that you should rely upon. If PHP ever updates how it processes a foreach() the behavior may change. Link to comment https://forums.phpfreaks.com/topic/270097-foreach-all-but-last-element/#findComment-1388881 Share on other sites More sharing options...
nodirtyrockstar Posted October 31, 2012 Author Share Posted October 31, 2012 Thanks! Link to comment https://forums.phpfreaks.com/topic/270097-foreach-all-but-last-element/#findComment-1388882 Share on other sites More sharing options...
Jessica Posted October 31, 2012 Share Posted October 31, 2012 This is what implode is for... Link to comment https://forums.phpfreaks.com/topic/270097-foreach-all-but-last-element/#findComment-1388888 Share on other sites More sharing options...
haku Posted October 31, 2012 Share Posted October 31, 2012 $results = array(); foreach ($srch as $value) { $results[] = "`" . $value . "`"; } $output = implode(',', $results); $output will contain your string with the results separated by a comma. Link to comment https://forums.phpfreaks.com/topic/270097-foreach-all-but-last-element/#findComment-1388917 Share on other sites More sharing options...
Christian F. Posted October 31, 2012 Share Posted October 31, 2012 Haku: If you're going to use implode (), why not take it all the way? $output = '`'.implode ('`, `', $srch).'`'; No need for the foreach () at all. Link to comment https://forums.phpfreaks.com/topic/270097-foreach-all-but-last-element/#findComment-1389010 Share on other sites More sharing options...
nodirtyrockstar Posted October 31, 2012 Author Share Posted October 31, 2012 Thanks everyone! I never used implode before, and this really helped to improve my code. Link to comment https://forums.phpfreaks.com/topic/270097-foreach-all-but-last-element/#findComment-1389020 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.