How to push multidimensional array to dropdown list so the array values become values in select lists

So I have multidimensional array like this:


$array = ['TITLE' => ['A', 'B1, B2','C']];

If I push it to dropDownList(), I will get html select tag where ‘TITLE’ will be optgroup label, and options of that optgroup will be this:




<option value="0">A</option>

<option value="1">B1, B2</option>

<option value="2">C</option>



I want my options values to contain option labels as values, not 0,1,2… because 0,1,2 is useless to me, I need those values to store them in database.

So I need options to look like this:




<option value="A">A</option>

<option value="B1, B2">B1, B2</option>

<option value="C">C</option>



How can I do this ?

Maybe hiddenInput() and some js? When you select one option populate value with label (from dropdown) in hiddenInput using js.

When you submit ActiveForm you will be able to receive selected value from that hiddenInput.

Add the form field eg,

$form->field($model, ‘property’)->dropDownList($model->myFunction())

Inside your model do




function myFunction()

{

   return array_map(function($myArray){// write some code to return the elements you want to send to the dropdown list}, ['TITLE' => ['A', 'B1, B2','C']]);


}