在我的数据库中,
"email" : [
"amnop@mailinator.com",
"abc@mail.com"
],
当我print_r($model->email)
时,
它显示
Array ( [0] => amnop@mailinator.com [1] => abc@mail.com )
在我的GridView中,
<?= GridView::widget([
'dataProvider' => $dataProvider,
----
'columns' => [
-----
'price',
[
'attribute' => 'email',
'value' => function($model) {
//I need help here... I prefer any foreach function
}
],
-----
]
?>
我必须在同一列中显示所有电子邮件。该怎么做?
修改
我从数据库中获取值时使用ActiveDataprovider
。
答案 0 :(得分:2)
根据您要实现的目标,您可以使电子邮件数组内爆:
[
'attribute' => 'email',
'value' => function($model) {
if (is_array($model->email)) {
return implode(', ', $model->email);
}
return $model->email;
}
],
答案 1 :(得分:0)
假设一个数组为
$data = [
['email' => 'amnop@mailinator.com'],
['email' => 'abc@mail.com'],
...
['email' => 'youremail100@mail.com'],
];
您可以使用ArrayDataProvider
$provider = new ArrayDataProvider([
'allModels' => $data,
'pagination' => [
'pageSize' => 10,
],
'sort' => [
'attributes' => [ 'email'],
],
]);
照常发送数据提供者
因此在gridview中,您可以使用,
<?= GridView::widget([
'dataProvider' => $dataProvider,
----
'columns' => [
-----
'price',
[
'attribute' => 'email',
'value' => function($model) {
//I need help here...
}
],
-----
]
?>
您可以查看yii2指南https://www.yiiframework.com/doc/guide/2.0/en/output-data-providers 和文档https://www.yiiframework.com/doc/api/2.0/yii-data-arraydataprovider