• 10

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191


File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 191
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

My model FAQ has 4 attributes

* @property integer $id * @property string $chapter * @property string $question * @property string $answer

Right now my actionIndex function looks like

public function actionIndex()

    $faq = Faq::find()->all();

    $dataProvider = new ActiveDataProvider([
        'query' => Faq::find(),

    return $this->render('index', [
        'dataProvider' => $dataProvider,
        'faq' => $faq

How can I get an array of unique values of $chapter using Yii2 or PHP in Controller? Lets say in sql it looks like

SELECT DISTINCT chapter FROM ' faq_table'

This can be done like this:


If you want the results as a plain array instead of an array containing Faq models you can add asArray() before ->all().

Running the code below will show you it'll produce this exact query.


Extra comment. I also think it's better to remove the line $faq = Faq::find()->all(); and use $dataProvider->getModels() if you want to use the models. This way the query to fetch the data isn't run twice.

  • 15
Reply Report
      • 1
    • This query returns an array of active records. Does exists a way to return an array with the column values? ['chapter1','chapter2']

Warm tip !!!

This article is reproduced from Stack Exchange / Stack Overflow, please click

Trending Tags

Related Questions