Answer the question
In order to leave comments, you need to log in
How to merge arrays with ArrayHelper?
Actually the question in the following is the User model with the properties id, firstName, lastName. Through ArrayHelper I need to combine these fields, that is, something like that.
ArrayHelper::map(User::find()->asArray()->all(),'id','firstName lastName');
Answer the question
In order to leave comments, you need to log in
are there any other suggestions?
$form->field($model, 'test')->dropDownList(ArrayHelper::map(User::find()->asArray()->all(), 'id', function($models){
return $models['firstName'] . ' - ' . $models['lastName'];
}
)
)
Many options, the first one showed @slo_nik
The second one is the toArray method:
https://www.yiiframework.com/doc/guide/2.0/en/help...
$data = ArrayHelper::toArray(User::find()->all(), [
'app\models\User' => [
'id',
'names' => function ($model) {
return $model->firstName . ' ' . $model->lastName;
},
],
]);
ArrayHelper::map($data, 'id', 'names');
public function getFullName(){
return $this->firstName . ' ' . $this->lastName;
}
ArrayHelper::map(User::find()->all(), 'id', 'fullName');
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question