Php Yii2-选择2 span HTML标记插入下拉列表

Php Yii2-选择2 span HTML标记插入下拉列表,php,html,yii2,dropdown,select2,Php,Html,Yii2,Dropdown,Select2,我有点小问题。 我想在Kartik-Select2小部件的列表项中添加一个span HTML标记。 我的想法是,我的数据库中有一些颜色,我想在列表中显示您可以选择的颜色。不仅仅是nem。但我使用引导颜色和其他颜色,我有两列(颜色代码和类)。 我希望你能理解我想用这个实现什么 因此,我尝试创建一个匿名函数来构建listitem,并在span之后生成,但在return语句中,这将是一个字符串,PHP编译器无法解析这是一个HTML代码 所以我的问题是,我该怎么做?因为作为回报,我可以回报你一件事。所以

我有点小问题。 我想在Kartik-Select2小部件的列表项中添加一个span HTML标记。 我的想法是,我的数据库中有一些颜色,我想在列表中显示您可以选择的颜色。不仅仅是nem。但我使用引导颜色和其他颜色,我有两列(颜色代码和类)。 我希望你能理解我想用这个实现什么

因此,我尝试创建一个匿名函数来构建listitem,并在span之后生成,但在return语句中,这将是一个字符串,PHP编译器无法解析这是一个HTML代码

所以我的问题是,我该怎么做?因为作为回报,我可以回报你一件事。所以我不能还给你名字和标签。 所以,如果可以的话,请帮助我

我的颜色表是:

color_id PK int 
type varchar - the color type (bootstrap, hex color etc.) for grouping 
name varchar - the color name
color_code varchar - if the color is hex i store the hex code in here
class varchar - if its a bootstrap color i store the class name in here
我的代码是:

$colors = Color::find();
        if(!empty($this->types))
            $colors->andWhere(['type' => $this->types]);

        return Select2::widget([
            'name' => $this->name,
            'value' => $this->value,
            'model' => $this->model,
            'attribute' => $this->attribute,
            'data' => ArrayHelper::map(
                $colors->all(),
                'color_id',
//                'name',
                function ($array, $default){
                    /** @var $array Color */
                    if( !isset($array->color_code) && isset($array->class) ) {
                        return $array->name
                            . ' <span class="'
                            . $array->class
                            . '"></span>';
                    } elseif ( isset($array->color_code) && !isset($array->class) ) {
                        return $array->name
                            . ' <span style="background-color: '
                            . $array->color_code
                            . '"></span>';
                    }
                },
                'type'
            ),
            'options' => $this->options,
        ]);
$colors=Color::find();
如果(!empty($this->types))
$colors->andWhere(['type'=>$this->types]);
返回Select2::widget([
“名称”=>$this->name,
“值”=>$this->value,
'model'=>$this->model,
'attribute'=>$this->attribute,
“数据”=>ArrayHelper::map(
$colors->all(),
“颜色标识”,
//“姓名”,
函数($array,$default){
/**@var$数组颜色*/
如果(!isset($array->color_code)&&isset($array->class)){
返回$array->name
. ' ';
}elseif(isset($array->color_code)和&!isset($array->class)){
返回$array->name
. ' ';
}
},
“类型”
),
“选项”=>$this->options,
]);
我希望你能写下好的,我想要的。对不起,我的英语不是最好的:)