Array to string - I am confused

Hello,

From this page:

http://www.yiiframework.com/doc-2.0/guide-output-data-providers.html

It shows:




$provider = new ActiveDataProvider([

    'query' => Post::find(),

    'pagination' => [

        'pageSize' => 20,

    ],

]);


// get the posts in the current page

$posts = $provider->getModels();




When I tried to "echo $posts = $provider->getModels();", I am getting the error "Array to string conversion".

Could someone please give me a little help on this?

Thank you,

Ben




<?php

$posts = $provider->getModels();

echo $posts

?>



Strangely enough, still the same error with your solution:

PHP Notice – yii\base\ErrorException

Array to string conversion

Sure because $posts is an array.

Correct code is:




$posts = $provider->getModels();

var_dump($posts);

print_r($posts);



you may use foreach to fetch all post info

example




foreach($post as $key=>$val){

  echo $val['field_name_in_your_table'];

}