Sal*_*yaz 2 php arrays gridview yii2 yii2-advanced-app
即时通讯使用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]);
}
Run Code Online (Sandbox Code Playgroud)
在上面的代码我将xml转换为数组.该$阵列具有阵列和IM通过它来查看名为产品.现在我想在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
)
)
)
)
)
Run Code Online (Sandbox Code Playgroud)
如果数组包含dataProvider之类的数据.你可以使用arrayDataProvider http://www.yiiframework.com/doc-2.0/yii-data-arraydataprovider.html 例如(来自yii2指南):
Run Code Online (Sandbox Code Playgroud)$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'], ], ]);
数据提供者简要指南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]);
}
Run Code Online (Sandbox Code Playgroud)
请参阅上面的示例,其中数组$ 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;']],
],
]); ?>
Run Code Online (Sandbox Code Playgroud)