Level 4
Hello you can do something like this
$collection = collect(['name' => 'Desk', 'price' => 200]);
$collection->toArray();
Reference is https://laravel.com/docs/5.1/collections#method-toarray
6 likes
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Hi I have a collection array returned from a query and I want to convert it into an array.
Query :
$arr[$id] = App\Category::where('parent_category_id', $id)->lists('id');
what it returns
Array
(
[427] => Illuminate\Support\Collection Object
(
[items:protected] => Array
(
[0] => 277
[1] => 279
[2] => 426
[3] => 428
[4] => 429
[5] => 430
[6] => 431
[7] => 432
[8] => 433
[9] => 434
)
)
[280] => Illuminate\Support\Collection Object
(
[items:protected] => Array
(
[0] => 281
[1] => 282
[2] => 435
[3] => 436
[4] => 437
)
)
[283] => Illuminate\Support\Collection Object
(
[items:protected] => Array
(
[0] => 284
[1] => 285
[2] => 286
)
)
)
Now I want to convert it into an proper array
$arr = Array
(
"427" =>
Array
(
"0" => 277,
"1" => 279,
"2" => 426,
"3" => 428,
"4" => 429,
"5"=> 430,
"6" => 431,
"7" => 432,
"8" => 433,
"9" => 434
),
"280" =>
Array
(
"0" => 281,
"1" => 282,
"2" => 435,
"3" => 436,
"4" => 437
),
"283" => Array
(
"0" => 284,
"1" => 285,
"2" => 286
)
);
How will i convert it into a proper array. please assist.
Hello you can do something like this
$collection = collect(['name' => 'Desk', 'price' => 200]);
$collection->toArray();
Reference is https://laravel.com/docs/5.1/collections#method-toarray
Please or to participate in this conversation.