Answer the question
In order to leave comments, you need to log in
Yii arrayhelper how to return an array?
There is a method in the model
public static function getItemList($cat_id)
{
$models = NetItem::find()->where(['count_id' => $cat_id])->asArray()->all();
return ArrayHelper::map($models, 'id', 'name_en');
}
output: {244:Itemname, 245:Itemname2}
$out = [
['id'=>244, 'name'=>'Itemname'],
['id'=>245, 'name'=>'Itemname2'],
// and so on
];
Answer the question
In order to leave comments, you need to log in
public static function getItemList($cat_id)
{
return array_map(function (array $item) {
return [
'id' => $item['id'],
'name' => $item['name_en'];
];
}, NetItem::find()->where(['count_id' => $cat_id])->asArray()->all());
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question