【问题标题】:How to get yii select query values as array如何获取 yii 选择查询值作为数组
【发布时间】:2021-08-09 07:10:09
【问题描述】:

我在 yii 中很新,因为我们在 laravel 中使用 pluck() 来获取数组中的特定列值我如何在 yii 中做同样的事情。我期望数组中的值作为不同的对象返回

*********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]

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

【问题讨论】:

    标签: php yii2 laravel-7


    【解决方案1】:

    您可以使用 column 方法,它将返回每个结果的第一列。 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();
    

    或使用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);
    

    【讨论】:

      猜你喜欢
      • 2017-05-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-26
      • 1970-01-01
      • 2019-06-30
      相关资源
      最近更新 更多