从数组中删除双引号

时间:2019-07-21 10:56:42

标签: php laravel

我在Laravel中使用WhereIn()方法来获取各种'id',但是问题是数据正在获取,就像这样的["6,19"]我所期望的是一个带有数字且没有双引号的数组。

要获取的数据来自varchar字段,因此它以varchar形式出现,现在我需要将其更改为[6,19],不要使用双引号。请指导我这样做。

whereIn方法仅支持带数字的数组。

public function getusers($id)
{
    $users = User::where('member_id', $id)->get();

    foreach($users as $user)
    {
        $s = $user->path;

        $s = str_replace('/', ',', $s); //this gives 6,19 and if i wrap that with [] it gives us ["6,19"].

        return $usergs = User::whereIn("id", [$s])->get(); //[$s] gives like ["6,19"] -but i need to change that as [6,19] (array not string)
    }

}

1 个答案:

答案 0 :(得分:2)

您可以改为在/上爆炸,这使其成为一个数组。然后,您可以将该数组传递到whereIn()中,而无需替换任何内容。

public function getusers($id)
{
    $users = User::where('member_id', $id)->get();

    foreach($users as $user) {
        $s = explode("/", $user->path);

        return $usergs = User::whereIn("id", $s)->get();
    }

}