Skip to content
Advertisement

yii2:drop-down list for multiple values concat in one line

for my drop-down list I am using this code.

<?= $form->field($medicinerequest, '[' . $id . ']' . 'medicine_name')
->DropDownList(ArrayHelper::map(appmodelsMedicine::find()
->asArray()->all(), 'id', 'medicine_name','medicine_id' ),
[ 'prompt' => 'Please Select' ])?> 

I am getting the drop-down list as in the picture. But I want it to be concatenated by hyphen(-) in one line. How can I do this?

medicine-request-drop-down

Advertisement

Answer

ArrayHelper::map($array, $from, $to, $group) uses ArrayHelper::getValue() to obtain the values of $from, $to and $group. ArrayHelper::getValue() allows you to pass closures.

The anonymous function signature should be: function($array, $defaultValue).

As such you can set $to as

ArrayHelper::map(
    appmodelsMedicine::find()->asArray()->all(),
    'id',
    function($model) {
        return $model['medicine_name'].'-'.$model['medicine_id'];
    }
)
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement