Answer the question
In order to leave comments, you need to log in
YII2 array output in yii2-widget-select2?
Hello everybody! Tell me how to get an array and display it in yii2-widget-select2 ? At the moment I am getting an array like this:
$equipment = Type::find()->indexBy('id')->asArray()->all();
$result = ArrayHelper::map($equipment, 'id', 'name', 'parent_id');
Answer the question
In order to leave comments, you need to log in
you can try to bypass it somehow
$equipment = Type::find()->indexBy('id')->asArray()->all();
$equipment = array_map(function ($row) use ($equipment) {
$row['parent_name'] = isset($equipment[$row['parent_id']]) ? $equipment[$row['parent_id']]['name'] : null;
return $row;
}, $equipment);
$equipment = array_filter($equipment, function ($row) {
return $row['parent_name'];
});
$result = ArrayHelper::map($equipment, 'id', 'name', 'parent_name');
$equipment = Type::find()->indexBy('id')->asArray()->all();
$equipment = array_map(function ($row) use ($equipment) {
$row['parent_name'] = isset($equipment[$row['parent_id']]) ? $equipment[$row['parent_id']]['name'] : $row['parent_id'];
return $row;
}, $equipment);
$result = ArrayHelper::map($equipment, 'id', 'name', 'parent_name');
if(isset($result[0])) {
unset($result[0]);
}
Thank you! Your version of the code is ideal for solving the problem! After applying the code, there is only one question left, now all categories are displayed in the select, including that the parent categories are displayed in a separate category "0". Can you tell me how to remove them so that they are not displayed as a separate array "0", but at the same time they do not disappear as subheadings of device categories?
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question