Grimloch Posted July 12, 2009 Share Posted July 12, 2009 Hello all, I have a slight problem that I need help with. I am working on a php newsletter script. What I am trying to do is figure out how to format the output of an array (displays to the admin all of the email addresses that were sent-to); my array name is '$list'. As it is now say I select 20 users for receiving the newsletter/message. After it is sent the output to the admin screen would be: Message Sent To: xxx@x.com, xxx@x.com, xxx@x.com, etc. etc. etc. for a continuos horizontal line of 20 email addresses seperated by a comma and a space. What I need to do is manipulate the array dropping the ', ' (comma and a space), display 4 email addresses then break and display the next 4 etc. Any help would be greatly appreciated. Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted July 12, 2009 Share Posted July 12, 2009 If $list is an array. You can use array_chunk to cut your $list array into smaller arrays which hold 4 emails each. Then you can do $rows = array_chunk($list, 4); foreach($rows as $emails) { echo implode(', ', $emails) . '<br />'; } Quote Link to comment Share on other sites More sharing options...
ignace Posted July 12, 2009 Share Posted July 12, 2009 $email = array(); $sizeof = sizeof($list); for ($i = 1; $i <= $sizeof; ++$i) { if (!($i % 4)) { print implode(', ', $email); $email = array(); } $email[] = $list[$i - 1]; mail(..); } Quote Link to comment Share on other sites More sharing options...
.josh Posted July 12, 2009 Share Posted July 12, 2009 If $list is a string you can explode by the comma and follow prev posts or leave as string and do this: $list = preg_replace('~((?:[^,]*,){4})~','$1<br/>',$list); Quote Link to comment Share on other sites More sharing options...
Grimloch Posted July 12, 2009 Author Share Posted July 12, 2009 @Crayon Violet: Thanks but I'm there already! If $list is an array. You can use array_chunk to cut your $list array into smaller arrays which hold 4 emails each. Then you can do $rows = array_chunk($list, 4); foreach($rows as $emails) { echo implode(', ', $emails) . '<br />'; } Thanks 'wildteen88'. This completely solved my problem. Had to change it a little though; I forgot that $list was already a string derived from the array which is simply $array. So I changed it to this: array_chunk($array, 2); I decided on 2 addresses across instead of 4. This works perfectly! Thanks again ! 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.