Mr.Turtle
Mr.Turtle

Reputation: 3090

Find a value in a multi dimensional array and keep structure

I have a multi dimensional array that contains a hierarchy of a navigation. I want to search for the name key for a specific element. There can be x amounts of levels in the hierarchy.

I want to search in the multi dimensional array and return a filtered multi dimensional array (with the same structure), only with the searched values. I have looked at different solutions, but they only seems to return the value.

Any help is appreciated.


This is my array now:

Array
(
    ...

    [60] => Array
        (
            [term_id] => 60
            [name] => Vinylbiler
            [slug] => vinylbiler
            [term_group] => 0
            [term_taxonomy_id] => 60
            [taxonomy] => toys
            [description] => 
            [parent] => 0
            [count] => 2
            [filter] => raw
            [children] => Array
                (
                    [63] => Array
                        (
                            [term_id] => 63
                            [name] => 1:43 serien
                            [slug] => 143-serien
                            [term_group] => 0
                            [term_taxonomy_id] => 63
                            [taxonomy] => toys
                            [description] => 
                            [parent] => 60
                            [count] => 2
                            [filter] => raw
                            [children] => Array
                                (
                                    [64] => Array
                                        (
                                            [term_id] => 64
                                            [name] => Gul
                                            [slug] => gul
                                            [term_group] => 0
                                            [term_taxonomy_id] => 64
                                            [taxonomy] => toys
                                            [description] => 
                                            [parent] => 63
                                            [count] => 1
                                            [filter] => raw
                                            [children] => Array
                                                (
                                                )

                                        )

                                    [65] => Array
                                        (
                                            [term_id] => 65
                                            [name] => Rød
                                            [slug] => rod
                                            [term_group] => 0
                                            [term_taxonomy_id] => 65
                                            [taxonomy] => toys
                                            [description] => 
                                            [parent] => 63
                                            [count] => 1
                                            [filter] => raw
                                            [children] => Array
                                                (
                                                )

                                        )

                                )

                        )

                )

        )

)

If I search for "Gul" I want to have an array looking like this:

    [60] => Array
        (
            [term_id] => 60
            [name] => Vinylbiler
            [slug] => vinylbiler
            [term_group] => 0
            [term_taxonomy_id] => 60
            [taxonomy] => toys
            [description] => 
            [parent] => 0
            [count] => 2
            [filter] => raw
            [children] => Array
                (
                    [63] => Array
                        (
                            [term_id] => 63
                            [name] => 1:43 serien
                            [slug] => 143-serien
                            [term_group] => 0
                            [term_taxonomy_id] => 63
                            [taxonomy] => toys
                            [description] => 
                            [parent] => 60
                            [count] => 2
                            [filter] => raw
                            [children] => Array
                                (
                                    [64] => Array
                                        (
                                            [term_id] => 64
                                            [name] => Gul
                                            [slug] => gul
                                            [term_group] => 0
                                            [term_taxonomy_id] => 64
                                            [taxonomy] => toys
                                            [description] => 
                                            [parent] => 63
                                            [count] => 1
                                            [filter] => raw
                                            [children] => Array
                                                (
                                                )

                                        )

                                )

                        )

                )

        )

And If I search for "Vinylbiler" I want an array looking like this:

[60] => Array
    (
        [term_id] => 60
        [name] => Vinylbiler
        [slug] => vinylbiler
        [term_group] => 0
        [term_taxonomy_id] => 60
        [taxonomy] => toys
        [description] => 
        [parent] => 0
        [count] => 2
        [filter] => raw
        [children] => Array
            (    
            )

    )

Edit: I know that my question is vague and doesn't show any code attempts. I have tried different variations of array_search without luck. http://php.net/manual/en/function.array-search.php

Any help, links or pinpoints to where I can find help to this problem is highly appreciated.

Upvotes: 1

Views: 116

Answers (1)

Nick
Nick

Reputation: 147196

I think this function will give you the results you want. It recursively searches the array for a name value, returning the node where it was found or false if it wasn't. If the name is found at a given level, the children of that node are removed and the node returned, allowing the building of the tree to the node.

function search_array($array, $name) {
    foreach ($array as $key => $value) {
        if ($value['name'] == $name) {
            // found the term. remove any children of this value
            $value['children'] = array();
            return $value;
        }
        elseif (is_array($value['children'])) {
            if (($child = search_array($value['children'], $name)) !== false) {
                $value['children'] = $child;
                return $value;
            }
        }
    }
    return false;
}

The output from this is long so I haven't included it, but you can see it on the demo on 3v4l.org.

Note that the function returns false if a name is not found anywhere in the array.

Upvotes: 3

Related Questions