0

I have the following array :

Array
(
    [0] => Array
        (
            [id] => 1
            [text] => Vehicule
            [state] => Array
                (
                    [opened] => 1
                )

            [children] => Array
                (
                    [0] => Array
                        (
                            [id] => 2
                            [text] => Cars
                            [parent] => 1
                            [children] => Array
                            (
                                [0] => Array
                                (
                                [id] => 99
                                [text] => BMW
                                [parent] => 2
                            )

                        )

                    [1] => Array
                        (
                            [id] => 3
                            [text] => Bikes
                            [parent] => 1
                        )

                    [2] => Array
                        (
                            [id] => 12
                            [text] => Boat
                            [parent] => 1
                        )

                    [3] => Array
                        (
                            [id] => 2
                            [text] => Cars
                            [parent] => 1
                            [children] => Array
                            (
                                [0] => Array
                                (
                                [id] => 99
                                [text] => BMW
                                [parent] => 2
                            )
                        )

                    [4] => Array
                        (
                            [id] => 3
                            [text] => Bikes
                            [parent] => 1
                        )

                    [5] => Array
                        (
                            [id] => 12
                            [text] => Boat
                            [parent] => 1
                        )

                )

        )

    [1] => Array
        (
            [id] => 1
            [text] => Vehicule
            [state] => Array
                (
                    [opened] => 1
                )

            [children] => Array
                (
                    [0] => Array
                        (
                            [id] => 2
                            [text] => Cars
                            [parent] => 1
                            [children] => Array
                            (
                                [0] => Array
                                (
                                [id] => 99
                                [text] => BMW
                                [parent] => 2
                            )
                        )

                    [1] => Array
                        (
                            [id] => 3
                            [text] => Bikes
                            [parent] => 1
                        )

                    [2] => Array
                        (
                            [id] => 12
                            [text] => Boat
                            [parent] => 1
                        )

                    [3] => Array
                        (
                            [id] => 2
                            [text] => Cars
                            [parent] => 1
                            [children] => Array
                            (
                                [0] => Array
                                (
                                [id] => 99
                                [text] => BMW
                                [parent] => 2
                            )
                        )

                    [4] => Array
                        (
                            [id] => 3
                            [text] => Bikes
                            [parent] => 1
                        )

                    [5] => Array
                        (
                            [id] => 12
                            [text] => Boat
                            [parent] => 1
                        )

                )

        )

)

And here is my expected result :

Array
(
    [0] => Array
        (
            [id] => 1
            [text] => Vehicule
            [state] => Array
                (
                    [opened] => 1
                )

            [children] => Array
                (
                    [0] => Array
                        (
                            [id] => 2
                            [text] => Cars
                            [parent] => 1
                            [children] => Array
                            (
                                [0] => Array
                                (
                                [id] => 99
                                [text] => BMW
                                [parent] => 2
                            )

                        )

                    [1] => Array
                        (
                            [id] => 3
                            [text] => Bikes
                            [parent] => 1
                        )

                    [2] => Array
                        (
                            [id] => 12
                            [text] => Boat
                            [parent] => 1
                        )

                )

        )

)

I have tried using

$arr= array_map("unserialize", array_unique(array_map("serialize", $arr)));

But it remove only the first array duplicated, it doesn't drill down.

PHP snippet : https://onlinephp.io/c/f6521

executable
  • 3,365
  • 6
  • 24
  • 52

1 Answers1

0

This will result in your expected output

function reduceArray($array){
    foreach($array as $c=>$v){
        if(is_array($v)){
            $array[$c] = reduceArray($v);
        }
    }
    return array_map("unserialize", array_unique(array_map("serialize", $array)));
}
         
print_r(reduceArray($arr));

But if in a sub array are the same values, like:

$arr = [
  'foo'=>1,
  'bar'=>1
];

Then this code will not work anymore.

There is no save and easy solution to your problem. This is just a dirty solution.

OMi Shah
  • 5,768
  • 3
  • 25
  • 34
Foobar
  • 769
  • 1
  • 4
  • Duplicate/copied https://stackoverflow.com/a/3598326/5882307 ? – OMi Shah Nov 03 '22 at 16:11
  • @OMiShah No i dont copy stuff. And doing `unserialize` after going into a the sub arrays is bottom-up and not a top-down approach. Thanks for the vote. – Foobar Nov 03 '22 at 16:17