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

1

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

@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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.