gpt4 book ai didi

php - 如何在yii2中将数组传递给gridview

转载 作者:行者123 更新时间:2023-12-02 07:22:49 26 4
gpt4 key购买 nike

我正在使用 yii2 框架

我有从 Controller 到 View 的数组。

 public function actionProducts()
{
$product = Yii::$app->httpclient->get('http://localhost/MWSProducts/src/MarketplaceWebServiceProducts/Samples/GetMatchingProductSample.php');

$array1 = str_replace("ns2:","",$product);
$array=json_decode(json_encode(simplexml_load_string($array1)),true);

return $this->render('products',['array'=>$array]);
}

在上面的代码中,我将 xml 转换为数组。$array 有数组,我将它传递给名为 products 的 View 。现在我想在 Gridview 的 View 中显示该数组,因为我是 yii2 的新手,我无法执行此操作,它说我必须使用一些数据提供程序,但我不知道如何执行此操作。

任何人都可以帮助我如何在 gridview 中显示数组。谢谢

谢谢你的回答..

实际上它是一个亚马逊产品数组,我从产品 API 获取它,我们不能定义任何预定义属性,如 id 和 all,因为它对于每个产品都是不同的。这就是数组的样子。

  Array
(
[GetMatchingProductResult] => Array
(
[0] => Array
(
[@attributes] => Array
(
[ASIN] => 0886467918
[status] => Success
)

[Product] => Array
(
[Identifiers] => Array
(
[MarketplaceASIN] => Array
(
[MarketplaceId] => A21TJRUUN4KGV
[ASIN] => 0886467918
)

)

[AttributeSets] => Array
(
[ItemAttributes] => Array
(
[Author] => Kipling, Rudyard
[Binding] => Hardcover
[Edition] => Har/Cas
[Format] => Import
[Label] => Imprint unknown
[Languages] => Array
(
[Language] => Array
(
[0] => Array
(
[Name] => english
[Type] => Published
)

[1] => Array
(
[Name] => english
[Type] => Original Language
)

[2] => Array
(
[Name] => english
[Type] => Unknown
)

)

)

[Manufacturer] => Imprint unknown
[PackageDimensions] => Array
(
[Weight] => 1.74
)

[ProductGroup] => Book
[ProductTypeName] => ABIS_BOOK
[PublicationDate] => 1988-05-02
[Publisher] => Imprint unknown
[SmallImage] => Array
(
[URL] => http://ecx.images-amazon.com/images/I/412CsE6Mb8L._SL75_.jpg
[Height] => 75
[Width] => 50
)

[Studio] => Imprint unknown
[Title] => Jungle Book ("Read Along")
)

)

[Relationships] => Array
(
)

[SalesRankings] => Array
(
[SalesRank] => Array
(
[0] => Array
(
[ProductCategoryId] => book_display_on_website
[Rank] => 709468
)

[1] => Array
(
[ProductCategoryId] => 1318084031
[Rank] => 14260
)

[2] => Array
(
[ProductCategoryId] => 1318083031
[Rank] => 47016
)

)

)

)

)

最佳答案

如果数组包含像 dataProvider 这样的数据。你可以使用 arrayDataProvider http://www.yiiframework.com/doc-2.0/yii-data-arraydataprovider.html例如(来自 yii2 指南):

    $data = [
['id' => 1, 'name' => 'name 1', ...],
['id' => 2, 'name' => 'name 2', ...],
...
['id' => 100, 'name' => 'name 100', ...],
];

$provider = new ArrayDataProvider([
'allModels' => $data,
'pagination' => [
'pageSize' => 10,
],
'sort' => [
'attributes' => ['id', 'name'],
],
]);

dataprovider 简要指南 http://www.yiiframework.com/doc-2.0/guide-output-data-providers.html

在你的情况下

    public function actionProducts()
{
$product = Yii::$app->httpclient->get('http://localhost/MWSProducts/src/MarketplaceWebServiceProducts/Samples/GetMatchingProductSample.php');

$array1 = str_replace("ns2:","",$product);
$array=json_decode(json_encode(simplexml_load_string($array1)),true);



$provider = new ArrayDataProvider([
'allModels' => $array,
'pagination' => [
'pageSize' => 10,
],
]);

return $this->render('products',['array'=>$array]);
}

引用上面的示例,其中数组 $data 包含 id、name 并假设您的 arrayDataProvider 在 var $array 中并且在 gridview 中也有 id、name 你应该有

 <?= GridView::widget([
'dataProvider' => $array,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'id',
'name',
.....

['class' => 'yii\grid\ActionColumn',
'contentOptions' => ['style' => 'width:84px; font-size:18px;']],
],
]); ?>

关于php - 如何在yii2中将数组传递给gridview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38178140/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com