我正在使用yii2框架
我有从控制器到视图的数组。
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 包含数组,我将其传递给名为产品的视图。现在我想在Gridview的视图中显示该数组,因为我是yii2的新手我无法做到这一点,它说我必须使用一些数据提供者,但我不知道如何做到这一点。
任何人都可以帮我这个如何在gridview中显示数组。谢谢
感谢您的回答..
实际上它是一个亚马逊产品阵列,我从Product 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
)
)
)
)
)
答案 0 :(得分:7)
如果数组包含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 $数组中并且在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;']],
],
]); ?>