As privily to merge arrays ArrayHelper?

0 like 0 dislike
41 views
Actually the question is have a User model with properties id, firstName, lastName. Using ArrayHelper I need to combine these fields that is something.
ArrayHelper::map(User::find()->asArray()->all(),'id','firstName lastName');

The only question is how to do it ?
by | 41 views

2 Answers

0 like 0 dislike
any other suggestions ?

To read the documentation said to yourself first and now repeat it to You))))
$form->field($model, 'test')->dropDownList(ArrayHelper::map(User::find()->asArray()->all(), 'id', function($models){ return $models['firstName'] . '- ' . $models['lastName']; } ) )
by
0 like 0 dislike
A lot of options, first shown @slo_nik
Second, is the method toArray:
\rhttps://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');

But it is better to create a getter in the model User.
public function getFullName(){ return $this->firstName . '' . $this->lastName; }

and then:
ArrayHelper::map(User::find()->all(), 'id', 'fullName');
by

Related questions

0 like 0 dislike
2 answers
asked Apr 13, 2019 by Sergalas
0 like 0 dislike
2 answers
0 like 0 dislike
4 answers
asked Jun 13, 2019 by beefront17
0 like 0 dislike
1 answer
asked May 20, 2019 by webentoru
0 like 0 dislike
2 answers
110,608 questions
257,186 answers
0 comments
33,673 users