array_chunk in email body php

Home / Uncategorized / array_chunk in email body php

I have a form where user can click on a button to add as many partecipants as he needs. For every partecipant I have 4 fields. Those fields are automatically added with a script when I click on the button to add partecipants. The fields are created as [data] so I have this in my php file to send the email:if(isset($_REQUEST[‘data’])){
$postdata = $_REQUEST[‘data’];

if( isset($postdata) ) {
$altri = implode(‘

‘, array_chunk($postdata, 4, true));

else {
$altri = ‘Non sono stati aggiunti altri partecipati a questa richiesta’;

then in my $body I try to print the result as:$body = <<Altri partecipanti:

// PHP email sender
mail($to, $sub, $body, $headers);

As you can see I try to group and separate those 4 fields in the email output to obtain something like:field_1_participant_1


… etc…

I’m not able to let it work as intended and I got an error message for "Array to string conversion"

How could I write the code to obtain it? Thanks in advance.

Think about what array_chunk does. It produces an array of arrays, eg
[ [‘f1p1’, ‘f2p1’, ‘f3p1’, ‘f4p1’], [‘f1p2’, ‘f2p2’, ‘f3p2’, ‘f4p2’] ]

Try using array_map to convert the inner arrays into strings. For example
$altri = implode(‘

‘, array_map(function($list) {
return ‘

  • ‘ . implode(‘
  • ‘, $list) . ‘

}, array_chunk($postdata, 4)));

This produces

  • f1p1
  • f2p1
  • f3p1
  • f4p1

  • f1p2
  • f2p2
  • f3p2
  • f4p2

Read more

Leave a Reply

Your email address will not be published. Required fields are marked *