Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

eddy1992's avatar

How to convert this collection to an array ?

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.

0 likes
2 replies

Please or to participate in this conversation.