Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel - Remove elements having NULL value from multidimentional array

I am using Laravel 5.3.

I have a multidimensional array like:

Array
(
    [id] => 37141
    [last_done_on] => []
    [children] => Array
        (
            [0] => NULL /* This must be removed */
            [1] => Array
                (
                    [id] => 37142
                    [last_done_on] => Array()
                    [children] => Array()
                )

            [2] => Array
                (
                    [id] => 37143
                    [last_done_on] => Array()
                    [children] => Array
                        (
                            [0] => Array
                                (
                                    [id] => 37144
                                    [last_done_on] => Array()
                                    [children] => Array()
                                )
                            [1] => Array
                                (
                                    [id] => 37145
                                    [last_done_on] => Array()
                                    [children] => Array()
                                )
                        )
                )
            [3] => Array
                (
                    [id] => 37157
                    [last_done_on] => Array()
                    [children] => Array
                        (
                            [0] => Array
                                (
                                    [id] => 37158
                                    [last_done_on] => Array()
                                    [children] => Array()
                                )
                            [1] => Array
                                (
                                    [id] => 37159
                                    [last_done_on] => Array()
                                    [children] => Array
                                        (
                                            [0] => NULL  /* This must be removed */
                                        )
                                )
                        )
                )
        )
)

And I want to remove the elements that are NULL. So the result should be like:

Array
(
    [id] => 37141
    [last_done_on] => []
    [children] => Array
        (
            [0] => Array
                (
                    [id] => 37142
                    [last_done_on] => Array()
                    [children] => Array()
                )

            [1] => Array
                (
                    [id] => 37143
                    [last_done_on] => Array()
                    [children] => Array
                        (
                            [0] => Array
                                (
                                    [id] => 37144
                                    [last_done_on] => Array()
                                    [children] => Array()
                                )
                            [1] => Array
                                (
                                    [id] => 37145
                                    [last_done_on] => Array()
                                    [children] => Array()
                                )
                        )
                )
            [2] => Array
                (
                    [id] => 37157
                    [last_done_on] => Array()
                    [children] => Array
                        (
                            [0] => Array
                                (
                                    [id] => 37158
                                    [last_done_on] => Array()
                                    [children] => Array()
                                )
                            [1] => Array
                                (
                                    [id] => 37159
                                    [last_done_on] => Array()
                                    [children] => Array
                                        (
                                        )
                                )
                        )
                )
        )
)

How to do this?

like image 406
Dev Avatar asked Mar 16 '17 08:03

Dev


1 Answers

In collection, use filter

some_collection->filter(function($value, $key) {
    return  $value != null;
});
like image 180
Ganesh K Avatar answered Nov 15 '22 20:11

Ganesh K