yii2:一行中多个值concat的下拉列表

Paw*_*wan 22 php yii2

对于我的下拉列表,我正在使用此代码.

<?= $form->field($medicinerequest, '[' . $id . ']' . 'medicine_name')
->DropDownList(ArrayHelper::map(\app\models\Medicine::find()
->asArray()->all(), 'id', 'medicine_name','medicine_id' ),
[ 'prompt' => 'Please Select' ])?> 
Run Code Online (Sandbox Code Playgroud)

我正在获取图片中的下拉列表.但是我希望它在一行中用连字符( - )连接起来.我怎样才能做到这一点?

医药请求,下拉

top*_*her 49

ArrayHelper::map($array, $from, $to, $group)使用ArrayHelper::getValue()得到的值$from,$to$group.ArrayHelper::getValue()允许你传递闭包.

匿名函数签名应该是:function($array, $defaultValue).

因此你可以设置$to

ArrayHelper::map(
    \app\models\Medicine::find()->asArray()->all(),
    'id',
    function($model) {
        return $model['medicine_name'].'-'.$model['medicine_id'];
    }
)
Run Code Online (Sandbox Code Playgroud)