ArrayDataProvider is a data provider that implements the yii\data\DataProviderInterface. It provides data from a PHP array.
ArrayDataProvider is best used to provide data when the format of the data is known and can be specified as an array. It is especially useful when data needs to be sorted or paginated.
ArrayDataProvider can be configured with the following properties:
allModels
: array, the data array to be used as the data source.key
: string, the key that should be used to index the data items in the array.sort
: yii\data\Sort, the sorting object.pagination
: yii\data\Pagination, the pagination object.totalCount
: int, the total number of data items.options
: array, the additional options to be passed to the data provider.
ArrayDataProvider can be used in the following way:
$dataProvider = new yii\data\ArrayDataProvider([
'allModels' => $data,
'sort' => [
'attributes' => ['id', 'name', 'age'],
],
'pagination' => [
'pageSize' => 10,
],
]);
The above code will create an ArrayDataProvider that will provide data from the $data array, sorted by the 'id', 'name' and 'age' attributes, with a page size of 10.