2

I very new in yii as we use pluck() in laravel to get specific column value in a array how can i do same in yii.I am expecting values in array it return as different objects

*********My query **********
$get_coupon_ids=Coupon_categories::find()->select(['coupon_id'])->where(['in', 'category',$category_id])->all();

*****************************

*********OutPut****************

[
        {
            "coupon_id": 13
        },
        {
            "coupon_id": 14
        },
        {
            "coupon_id": 15
        }
]

***************************************

*******Expected value *************

[13,14,15]

*********************************

1 Answer 1

5

You can use the column method, it will return the first column of each result. https://www.yiiframework.com/doc/api/2.0/yii-db-query#column()-detail

$get_coupon_ids = Coupon_categories::find()
  ->select(['coupon_id'])
  ->where(['in', 'category', $category_id])
  ->column();

or use array_map

$get_coupon_ids = Coupon_categories::find()
  ->select(['coupon_id'])
  ->where(['in', 'category', $category_id])
  ->all();

$ids = array_map(fn($id) => $id['coupon_id'], $get_coupon_ids);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.