nodirtyrockstar Posted October 31, 2012 Share Posted October 31, 2012 (edited) 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(). Edited October 31, 2012 by nodirtyrockstar Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted October 31, 2012 Share Posted October 31, 2012 What's in the array? Quote Link to comment 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. Quote Link to comment Share on other sites More sharing options...
nodirtyrockstar Posted October 31, 2012 Author Share Posted October 31, 2012 Thanks! Quote Link to comment Share on other sites More sharing options...
Jessica Posted October 31, 2012 Share Posted October 31, 2012 This is what implode is for... Quote Link to comment Share on other sites More sharing options...
haku Posted October 31, 2012 Share Posted October 31, 2012 (edited) $results = array(); foreach ($srch as $value) { $results[] = "`" . $value . "`"; } $output = implode(',', $results); $output will contain your string with the results separated by a comma. Edited October 31, 2012 by haku Quote Link to comment 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. Quote Link to comment 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. Quote Link to comment 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.