Skip to content
Advertisement

Flatten laravel relationship collection / array

I am having problem with array convert I went use laravel collection using map Or php array

Array
(
    [0] => Array
        (
            [id] => 3487
            [title_fa] => Father
            [code] => 01
            [father_id] => 0
            [webmaster_id] => 8
            [grandchildren] => Array
                (
                    [id] => 3488
                    [title_fa] => Child 1
                    [code] => 02
                    [father_id] => 3487
                    [grandchildren] => 

                )

        )
     [1] => Array
        (
            [id] => 3489
            [title_fa] => Father 2
            [code] => 03
            [father_id] => 3488
            [grandchildren] => Array
                (
                    [id] => 3490
                    [title_fa] => Child 2
                    [code] => 04
                    [father_id] => 3489
                    [grandchildren] => 
                )

        )

)

array convert to array

Array
(
   [0] => Array
     (
            [title_fa] => Father
            [code] => 01
     )
   [1] => Array
     (
            [title_fa] => Father>Child1,
            [code] => 0102
     )
   [2] => Array
     (
            [title_fa] => Father2
            [code] => 03
     )
   [3] => Array
     (
            [title_fa] => Father2>Child2
            [code] => 0304
     )
)

IlluminateSupportCollection::filter()

JavaScript

Advertisement

Answer

Though the answer is already given but the output is not the desired output of the OP.

This can be solved using recursionlike this:

JavaScript

Output:

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement