A
A
agent11562017-01-24 11:25:58
Yii
agent1156, 2017-01-24 11:25:58

How to run an Active Record object through a loop?

$alana_first = Alaniatape::find()->where(['category' => "ALANÆ LIFE",'conclusion_top' => '1'])->orderBy('id DESC')->all();

<?php foreach($alana_first as $key => $value){
    echo $key;
    
}?>

Why can't I print $key ?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Maxim Timofeev, 2017-01-24
@webinar

Firstly, it's not a fact that you received an array and not null, maybe there are no records corresponding to the request.
Secondly, you got an array of objects, if you need a pure array, then you should use asArray()
And finally - just look at what's in the array:

<pre>
<?php print_r($alana_first); ?>
</pre>

N
Nikolai Konyukhov, 2017-01-24
@heahoh

Trying to display $key in foreach what do you want to see? Record primary key? To do this, you need to do $value->primaryKey

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question