Answer the question
In order to leave comments, you need to log in
Why doesn't foreach work in yii2?
Everything is displayed through the wardump, so $news1[0]["title"] is also displayed, but not through foreach.
<?php
echo "<pre>";
echo var_dump($news1);
echo "</pre>";
//?>
<?//=$news1[0]["title"]?>
<?//=$news1[1]["title"]?>
<?//=$news1[2]["title"]?>
<?foreach ($news1 as $val){?>
<?=$val?>
<?}?>
public function actionNews1()
{
$news1 = News1::find()->all();
return $this->render('news1',[
'news1' => $news1
]);
}
namespace app\models;
use Yii;
use yii\db\ActiveRecord;
class News1 extends ActiveRecord{
}
array(3) {
[0]=>
object(app\models\News1)#67 (8) {
["_attributes":"yii\db\BaseActiveRecord":private]=>
array(3) {
["id"]=>
int(1)
["title"]=>
string(18) "Страница 1"
["content"]=>
string(23) " содержимое 1"
}
["_oldAttributes":"yii\db\BaseActiveRecord":private]=>
array(3) {
["id"]=>
int(1)
["title"]=>
string(18) "Страница 1"
["content"]=>
string(23) " содержимое 1"
}
["_related":"yii\db\BaseActiveRecord":private]=>
array(0) {
}
["_errors":"yii\base\Model":private]=>
NULL
["_validators":"yii\base\Model":private]=>
NULL
["_scenario":"yii\base\Model":private]=>
string(7) "default"
["_events":"yii\base\Component":private]=>
array(0) {
}
["_behaviors":"yii\base\Component":private]=>
array(0) {
}
...
}
}
Answer the question
In order to leave comments, you need to log in
<?php foreach ($news1 as $val){?>
<?= var_dump($val); ?>
<? } ?>
<?php foreach ($news1 as $val){?>
<?= $val->title; ?>
<? } ?>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question