checkboxList array index

Is it possible to customise the array index generated by checkboxList()? For example:


<?php

$interests = [

    [

        'id' => 1,

        'title' => 'title1',

        'value' => 'value1',

    ],

    [

        'id' => 2,

        'title' => 'title2',

        'value' => 'value2',

    ],

];

?>


<?php echo $form->field($model, 'interests')->checkboxList($interests, [

    'item' => function($index, $label, $name, $checked, $value) {

        $return = '<div class="option">';

        $return .= Html::checkbox($name, $checked, ['value' => $label['value'], 'id' => 'option-' . $label['id']]);

        $return .= Html::label($label['title'], 'option-' . $label['id']);

        $return .= '</div>';

    },

]); ?>

The checkbox is generated as follows:


<input type="checkbox" name="Form[interests][]" value="1">

I want the following:


<input type="checkbox" name="Form[interests][title1]" value="1">

In other words, I want the array index to contain a value, rather than a zero-based numerical index.

why don’t you generate the name manually here is your example modified




'item' => function($index, $label, $name, $checked, $value) {

		$key = $interests[$index]['title'];

        $return = '<div class="option">';

        $return .= Html::checkbox("Form[interests][{$key}]", $checked, ['value' => $label['value'], 'id' => 'option-' . $label['id']]);

        $return .= Html::label($label['title'], 'option-' . $label['id']);

        $return .= '</div>';

    }

@alrazi I was hoping for a more "elegant" way of doing it, but I suppose this will do for now. Thanks.