将php数组转换为逗号分隔的字符串列表

时间:2016-08-29 14:28:49

标签: php arrays laravel

我有以下数组:

implode(',', $arr)

我的db类的select函数采用以下格式的参数:

MyModel.where('model_field ~* ?', 'some very very long regex')

如何转换我的数组以在select函数中传递这些值?

我尝试REG_ETOOBIG并且它给出了一个逗号分隔值的字符串,但我想要的不是单个字符串,它是一个逗号分隔字符串列表,如上例所示。

感谢您的帮助。

2 个答案:

答案 0 :(得分:3)

select()可以接受数组,请尝试:

DB::table('users')->select($arr);

这是source code of select() method

public function select($select = null)
{
    ....

    // In this line Laravel checks if $select is an array. If not, it creates an array from arguments.
    $selects = is_array($select) ? $select : func_get_args();

答案 1 :(得分:0)

您可以在Laravel 5.8中执行以下操作:

DB::table('users')->select('username')->pluck('username')->unique()->flatten();

希望这会有所帮助。