0

I'm building a form. In that form I have a lot of checkboxes. Normally, It's possible to make an array of some checkboxes (with name attribute).

<input type="text" name="reservatie[0][1]" />
<input type="text" name="reservatie[0][2]" />
<input type="text" name="reservatie[1][1]" />

Normally I would have this result with print_r($_POST);

Array(
    [reservatie] => Array(
        [0] => Array (
            [1] => value
            [2] => value
            )
        )
        [1] => Array
        (
            [1] => value
        )
    )
)

I tried to add the name attribute, but the $_POST takes the structure of $form. But how can I add a sub-array structure in my results?

foreach($result as $time) {
                    $form['reservatie'][$item->id][$time->id] = array(
                          '#type' => 'checkbox',
                          '#return_value' => 1,
                          '#default_value' => 0,
                          '#prefix' => '<td>',
                          '#suffix' => '</td>',
                          '#attributes' => array('name' => 'reservatie[0]['.$time->id.']')          
                    );
                }

This is what I get:

array(42) {
  ["date"]=>
  string(10) "01/07/2016"
  ["reservatie[1][1]"]=>
  int(0)
  ["reservatie[1][2]"]=>
  int(0)
  ["reservatie[1][3]"]=>
  int(0)
  ["reservatie[1][4]"]=>
  int(0)
  ["reservatie[1][5]"]=>
  int(0)
  ["reservatie[1][6]"]=>
  int(0)
  ["reservatie[1][7]"]=>
  int(0)
  ["reservatie[1][8]"]=>
  int(0)
  ["reservatie[1][9]"]=>
  int(0)
  ["reservatie[2][1]"]=>
  int(0)
...
)

1 Answer 1

0

I managed to solve my own problem. It was a problem with the #tree. I have to set #tree in the parents to true..

So if you want an array with the following structure:

array {
    ["parent"]=>
     array(4) {
        [1]=>
        array(9) {
          [1]=>
          int(1)
          [2]=>
          int(0)
          [3]=>
          int(0)
          [4]=>
          int(0)
          [5]=>
          int(0)
          [6]=>
          int(0)
          [7]=>
          int(0)
          [8]=>
          int(0)
          [9]=>
          int(0)
        }
    }
}

I had to configure #tree for the parents (2 here), and not only for the children

['parent']['#tree'] = true
['parent'][1]['#tree'] = true

I figured it out after trying and reading https://www.drupal.org/node/48643 (Archived, but it worked for me ;) )

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.