Here's a multimensional array :

Array
(
    [0] => Array
        (
            [id] => 5
            [children] => Array
                (
                    [0] => Array
                        (
                            [id] => 1
                            [children] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 2
                                        )

                                )

                        )

                )

        )

    [1] => Array
        (
            [id] => 6
        )

)

I'm looking for a PHP function which could remove a parent or grand-parent array from a given "id" value. For example, if id == 2, I'd like to remove the closest array with the key named "children", something like that :

Array
(
    [0] => Array
        (
            [id] => 5
            [children] => Array
                (
                    [0] => Array
                        (
                            [id] => 1

                        )

                )

        )

    [1] => Array
        (
            [id] => 6
        )

)

If id == 5, the new array would be :

Array
(
    [0] => Array
        (
            [id] => 6
        )

)

Or if id == 6, the new array would be :

Array
(
    [0] => Array
        (
            [id] => 5
            [children] => Array
                (
                    [0] => Array
                        (
                            [id] => 1
                            [children] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 2
                                        )

                                )

                        )

                )

        )

  )

How do i do this with maybe a recursive function ? I would really appreciate some help ! Thanks ! :)

share|improve this question
    
So, what have you tried? The task looks pretty basic to do via recursion. – YakovL Aug 1 '16 at 13:58
    
Hello, I tried via recursion, but only "id" key is removed, not the array "parent" when it contains the "children" key – Fabien Aug 1 '16 at 14:23
    
@Fabien You cannot delete children if they are also parents – prgrm Aug 1 '16 at 14:53
    
I wanted to say : the array whose key is called "children" (cf the examples above) – Fabien Aug 1 '16 at 15:04

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.