Jump to content

[SOLVED] Array Manipulation


Grimloch

Recommended Posts

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:

[email protected], [email protected], [email protected], 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.

Link to comment
https://forums.phpfreaks.com/topic/165692-solved-array-manipulation/
Share on other sites

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 />';
}

@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 !

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.