在多维数组中查找值并保持结构

时间:2018-12-22 22:19:16

标签: php arrays

我有一个包含导航层次结构的多维数组。我想搜索特定元素的name键。层次结构中可以有x个级别。

我想在多维数组中进行搜索,并仅返回具有搜索值的过滤后的多维数组(具有相同的结构)。我看过不同的解决方案,但它们似乎只返回值。

感谢您的帮助。


这是我的阵列:

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
                                                (
                                                )

                                        )

                                )

                        )

                )

        )

)

如果我搜索“ Gul”,我想要一个看起来像这样的数组:

    [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
                                                (
                                                )

                                        )

                                )

                        )

                )

        )

如果我搜索“ Vinylbiler”,我想要一个看起来像这样的数组:

[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
            (    
            )

    )

编辑: 我知道我的问题含糊不清,没有显示任何代码尝试。我尝试过array_search的其他变体而没有运气。 http://php.net/manual/en/function.array-search.php

我们非常感谢您提供的任何帮助,指向我可以找到该问题的帮助的链接或精确定位。

1 个答案:

答案 0 :(得分:3)

我认为此功能将为您提供所需的结果。它递归地在数组中搜索name值,返回找到它的节点,否则返回false。如果在给定级别找到该名称,则将删除该节点的子节点,然后返回该节点,从而可以在该节点上构建树。

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;
}

此命令的输出很长,因此我没有包含它,但是您可以在demo on 3v4l.org上看到它。

请注意,如果在数组中的任何位置都找不到name,则该函数将返回false。