Array to String Conversion: Checkbox Group Issue

I’m having some trouble getting checkbox group data array to post in an email. The data array exists, but I can’t get the checkbox group to break down into a string. I thought ‘implode’ would’ve fixed it, but I’m still getting the error: Notice: Array to string conversion…on line 6. Line six is the line that begins with $session.

<?php
    if ( !empty($form->data['student']) && is_array($form->data['student'] ) ) {
      $session = array();
      foreach ( $form->data['student'] as $s ) {
      $session[] = "<tr><td>{$s['schoosedayM']}</td></tr><tr><td>{$s['sxcareM']}</td></tr>";
      }
    }
    $form->data['session_data'] = implode("\n", $session);
    ?>

The data is here:

    Array
(
    [option] => com_chronoforms5
    [chronoform] => campmin
    [event] => submit
    [Itemid] => 
    [student] => Array
        (
            [1] => Array
                (
                    [schoosedayM] => Array
                        (
                            [0] => Full Day
                            [1] => Half Day (AM)
                            [2] => Half Day (PM)
                            [3] => None
                        )

                    [sxcareM] => Array
                        (
                            [0] => 8 to 9 AM
                            [1] => 12 to 1 PM
                            [2] => 4 to 5:30 PM
                            [3] => None
                        )

                )

        )

But nothing posts in the email:

[session_data] => <tr><td>Array</td></tr><tr><td>Array</td></tr>

Any ideas?

“Array” is not “nothing”
If you look at schoosedayM and sxcareM notice that they are Arrays with keys 0-3

Yes, but I don’t understand why my code is not breaking it down into strings.

You need to go one level deeper.

Which of the four are you wanting?
eg. if you want “half day PM” you should use

$session[] = "<tr><td>{$s['schoosedayM'][2]}</td></tr><tr><td>{$s['sxcareM'][2]}</td></tr>";

Well the choice is matter of the user. The user has the ability to choose as many checkbox choices as they want. Therefore, the code should be able to post all that have been chose.

The array above shows how many checkbox items have been chosen.

Unless I’m not understanding your question.

Ah, thanks, I missed that.
In that case I think having another !empty && is_array foreach for the $s arrays should work…

If it helps, the field name of one of the checkbox groups is [schoosedayM].

Yes I saw that. What I meant was to have something like

    if ( !empty($s['schoosedayM']) && is_array($s['schoosedayM'] ) ) {
      foreach ( $s['schoosedayM'] as $value_string ) {

inside the existing foreach and put together the table row at that point.

The code needs to go as deep into the data array as the values you want are nested in it.

Okay, I’m getting closer. I’ve managed to ‘implode’ the arrays within the $session. When an additional user is added, the arrays appear and post in the email. But if a user decides not to add any additional students, I get the
“Notice: Undefined variable: session on line 11”
“Warning: implode(): Invalid arguments passed”

    <?php
    if ( !empty($form->data['student']) && is_array($form->data['student'] ) ) {
      $session = array();
      foreach ( $form->data['student'] as $s ) {
       $session[] = "<tr><td><strong>Name:</strong></td> <td>{$s['cfname']} {$s['clname']}</td></tr><tr><td><strong>DOB:</strong></td> <td>{$s['sdob']}</td></tr><tr><td><strong>Age:</strong></td> <td>{$s['sage']}</td></tr><tr><td><strong>Sex:</strong></td> <td>{$s['ssex']}</td></tr><tr><td><strong>Special Needs:</strong></td> <td>{$s['sspecialneeds']}</td></tr><tr><td><strong>Date:</strong></td> <td>{$s['sstartdate']}</td><td>{$s['senddate']}</td></tr><tr><td><strong>Monday</strong></td><td></td></tr><tr><td><strong>Full/Half Day:</strong></td> <td>".implode(",", $s['schoosedayM'])."</td></tr>
<tr><td><strong>Extended Care:</strong></td> <td>".implode(",", $s['sxcareM'])."</td></tr><tr><td><strong>Tuesday</strong></td><td></td></tr><tr><td><strong>Full/Half Day:</strong></td> <td>".implode(",", $s['schoosedayT'])."</td></tr><tr><td><strong>Extended Care: </strong></td> <td>".implode(",", $s['sxcareT'])."</td></tr><tr><td><strong>Wednesday</strong></td><td></td></tr><tr><td><strong>Full/Half Day:</strong></td> <td>".implode(",", $s['schoosedayW'])."</td></tr><tr><td><strong>Extended Care: </strong></td> <td>".implode(",", $s['sxcareW'])."</td></tr><tr><td><strong>Thursday</strong></td><td></td></tr><tr><td><strong>Full/Half Day:</strong></td> <td>".implode(",", $s['schoosedayTH'])."</td></tr><tr><td><strong>Extended Care: </strong></td> <td>".implode(",", $s['sxcareTH'])."</td></tr>
<tr><td><strong>Friday</strong></td><td></td></tr><tr><td><strong>Full/Half Day:</strong></td> <td>".implode(",", $s['schoosedayF'])."</td></tr>
<tr><td><strong>Extended Care: </strong></td> <td>".implode(",", $s['sxcareF'])."</td></tr>";
      }
    }
    $form->data['session_data'] = implode("\n", $session);
    ?>

None of the fields in the additional student area are required and the default value ‘None’ is checked. So I’m not sure why the error is passing.

The array is created inside the very first if() check, but the implode() is done outside of that check. So if none are checked, at that point you’re trying to implode an array you haven’t created, hence the error.

Thanks, droopsnoot. I figured as much. But since I’m out of time, I’ll have to disable error reporting temporarily until I can devote more time to finding better ‘if’ solution to exclude this group if there are not elements selected.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.