yii2 arraydataprovider

The Yii framework provides a class called ArrayDataProvider in the yii2 version. This class is used to provide data from an array to be used in data-related components like GridView or ListView. It allows you to work with arrays as if they were database tables.

To use ArrayDataProvider in Yii2, you need to follow these steps:

  1. Create an array of data that you want to display. For example:
$data = [
    ['id' => 1, 'name' => 'John', 'age' => 25],
    ['id' => 2, 'name' => 'Jane', 'age' => 30],
    ['id' => 3, 'name' => 'Bob', 'age' => 35],
  1. Instantiate the ArrayDataProvider class and pass the data array to its constructor:
$dataProvider = new \yii\data\ArrayDataProvider([
    'allModels' => $data,
    'pagination' => [
        'pageSize' => 10,

In the above example, we set the allModels property to the data array, and the pagination property to specify the number of items to display per page.

  1. Use the $dataProvider object in your GridView or ListView widget to display the data:
echo \yii\grid\GridView::widget([
    'dataProvider' => $dataProvider,
    'columns' => [

In this example, we use the GridView widget and set the dataProvider property to the $dataProvider object. We define the columns we want to display using the columns property.

That's it! The ArrayDataProvider will handle the pagination and sorting of the data for you, making it easy to work with arrays in Yii2.