V
V
Vladimir2017-01-10 20:33:28
Yii
Vladimir, 2017-01-10 20:33:28

How to access array element in yii2 array?

Good day!
When I select one record:

$date = Statprice::find()->select(['from_date', 'to_date'])->where(['city' => 'Город'])->all();

That I can address here so:
$date->from_date;
And how to select record when I do selection of all records?
$date = Statprice::find()->select(['from_date', 'to_date'])->where(['city' => 'Город'])->all();

This, of course, does not fit:
$date->from_date;
do not offer foreach. He doesn't fit. How can such a selection be made?
Thanks in advance!

Answer the question

In order to leave comments, you need to log in

4 answer(s)
M
Maxim Fedorov, 2017-01-11
@MasterGerold

it's not entirely clear what you want to do when you select one record you get an OBJECT when you select all records you get an ARRAY OF OBJECTS, with which you can work like with a regular array - process in a loop, or for example access by index $date[0]-> from_date. Describe the task more specifically - what do you plan to do with this array, etc.

B
Boris Yakushev, 2017-01-10
@za4me

If memory serves, then in your case you need to get the data as an array.

E
Evgeny Bukharev, 2017-01-10
@evgenybuckharev

You can select from_date of all records using the ArrayHelper helper, it has a column () method

M
Maxim Timofeev, 2017-01-11
@webinar

It's hard to understand what you want. But apparently this:

$arr = Statprice::find()->all();
$from_date_collumn = ArrayHelper::getColumn($arr, 'from_date')

Everything is in the docs: www.yiiframework.com/doc-2.0/guide-helper-array.ht...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question