Let's say I have an array like this:
$mail[0] = "test1@gmail.com";
$mail[1] = "test2@gmail.com";
$mail[2] = "test3@gmail.com";
$mail[3] = "test4@gmail.com";
$mail[4] = "test5@gmail.com";
$mail[5] = "test6@gmail.com";
Now I want to convert these mails to string and put them in array after every 3 mail of $mail separated by a comma. Something like this -
$email[0] = "test1@gmail.com, test2@gmail.com, test3@gmail.com";
$email[1] = "test4@gmail.com, test5@gmail.com, test6@gmail.com";
$email[2] = "test71@gmail.com, test8@gmail.com, test9@gmail.com";
How can I do this ?
Slight modification of your code:
for($i=0; $i<sizeof($oneusers); $i+=3){
$new[] = $oneusers[$i].", ".$oneusers[$i + 1].", ".$oneusers[$i + 2];
}
print_r($new);
Going further you can add additional check whether $oneusers[key]
exists.
Another code, which will do the same but without writing i, i+1, i+2
:
$chunks = array_chunk($oneusers, 3);
foreach ($chunks as $chunk) {
$new[] = implode(',', $chunk);
}
print_r($new);