0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

DB::select で作られる中途半端な Array を完全な Array にする

Posted at

DB::select を使ったときは以下ように、配列の中にオブジェクトが存在します。

>>> $a = DB::select("select id, name from companies limit 2");
=> [
     {#4090
       +"id": 1,
       +"name": "Hoge",
     },
     {#4091
       +"id": 2,
       +"name": "Piyo",
     },
   ]

これを完全な配列にする方法は2つあります。

方法1

>>> json_decode(json_encode($a), true);
=> [
     [
       "id" => 1,
       "name" => "Hoge",
     ],
     [
       "id" => 2,
       "name" => "Piyo",
     ],
   ]

方法2

>>> foreach($a as &$v){$v = collect($v)->toArray();}
>>> $a;
=> [
     [
       "id" => 1,
       "name" => "Hoge",
     ],
     [
       "id" => 2,
       "name" => "Piyo",
     ],
   ]

以上です。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?