A
A
akula222018-12-28 15:27:06
Yii
akula22, 2018-12-28 15:27:06

How to create multidimensional array to put it in dropDownList (yii2)?

There is a table of news and their categories.
request

find()->with(['category'])->orderBy(['position' => SORT_DESC])->all()

making a connection
public function getCategory()
    {
        return $this->hasOne(NewsCategory::className(), ['id' => 'cat_id']);
    }

I need to get this array
'cat1' => [
                1 => 'news_title1',
                2 => 'In news_title2',        
            ],
            'cat2' => [
                3 => 'news 3',             
            ],

To then output it to dropDownList
Where cat1 is the headings from the category table and news is the headings from the news table,
tell me how to do this?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
akula22, 2018-12-28
@akula22

There are miracles, just wrote on the ball like this
and everything turned out)))

D
developer007, 2018-12-28
@developer007

make
helper/CategoryDropDown with toArray(..) method
it returns an array using foreach as an example

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question