简体   繁体   English

在PHP中的多维数组中搜索过滤器

[英]Search filter in multidimensional array in php

How would I create a search function that search in multidimensional nested array? 如何创建在多维嵌套数组中搜索的搜索功能?

Given the following array : 给定以下数组:

Array
(
[0] => Array
    (
        [id] => 1
        [parent_name] => ACTIVITIES
        [children] => Array
            (
            )

    )

[1] => Array
    (
        [id] => 2
        [parent_name] => ANIMALS/PETS
        [children] => Array
            (
            )

    )

[2] => Array
    (
        [id] => 3
        [parent_name] => ART
        [children] => Array
            (
                [0] => Array
                    (
                        [post_id] => 100
                        [child_name] => Bookeeping
                        [painting] => Array
                            (
                                [0] => flower
                                [1] => beach
                                [2] => sunrise
                            )

                    )

                [1] => Array
                    (
                        [post_id] => 101
                        [child_name] => Addiction
                        [painting] => Array
                            (
                                [0] => sunrise
                                [1] => beach
                            )

                    )

            )

    )
[3] => Array
    (
        [id] => 4
        [parent_name] => Music
        [children] => Array
            (
                [0] => Array
                    (
                        [post_id] => 102
                        [child_name] => POP
                        [painting] => Array
                            (
                                [0] => suntown
                                [1] => beachfull
                                [2] => sunrise
                            )

                    )

                [1] => Array
                    (
                        [post_id] => 103
                        [child_name] => Rock
                        [painting] => Array
                            (
                                [0] => sunrisenew
                                [1] => beachnew
                            )

                    )

            )

    )
)

How would I filter this array with a key which search in 'painting'? 如何在“绘画”中使用搜索关键字过滤此数组?

For example search key is "sun" 例如,搜索关键字为“ sun”

Result should be like this : 结果应该是这样的:

Array
(

[2] => Array
    (
        [id] => 3
        [parent_name] => ART
        [children] => Array
            (
                [0] => Array
                    (
                        [post_id] => 100
                        [child_name] => Bookeeping
                        [painting] => Array
                            (
                                [2] => sunrise
                            )

                    )

                [1] => Array
                    (
                        [post_id] => 101
                        [child_name] => Addiction
                        [painting] => Array
                            (
                                [0] => sunrise
                            )

                    )

            )

    )
[3] => Array
    (
        [id] => 4
        [parent_name] => Music
        [children] => Array
            (
                [0] => Array
                    (
                        [post_id] => 102
                        [child_name] => POP
                        [painting] => Array
                            (
                                [0] => suntown
                            )

                    )

                [1] => Array
                    (
                        [post_id] => 103
                        [child_name] => Rock
                        [painting] => Array
                            (
                                [0] => sunrisenew
                            )

                    )

            )

    )
)
$needle = "sun";
$filteredArr = array_filter(
    $arr,
    function($v) use ($needle) {
        foreach($v['children'] as $child) {
            // "Version 1" of filtering, no "parts of strings" searches
            if (in_array($needle, $child['painting'])) {
                return true;
            }

            // "Version 2" of filtering, keyword may be part of a painting string
            foreach($child['painting'] as $painting) {
                if (strpos($painting, $needle) !== false) {
                    return true;
                }
            }
        }

        return false;
    }
);

If you use the callback for array_filter() then you can do fairly advanced filterings. 如果对array_filter()使用回调,则可以进行相当高级的过滤。

Edit New version of answer for removing the entries in painting arrays not matching the pattern. 编辑新版本的答案,以删除绘画阵列中与模式不匹配的条目。

This works by doing nested foreach loops, all the way in to the innermost array needed, painting in this case. 通过执行嵌套的foreach循环(一直到所需的最里面的数组)进行工作,在这种情况下进行绘画 The painting array is filtered so that all non matching elements in it are removed. 绘画数组被过滤,以便删除其中所有不匹配的元素。 When done with this, at the next level outwards, the painting array is checked, and if empty the removal kind of "propagates outwards". 完成此操作后,在下一个向外的级别检查绘画数组,如果为空,则除去类型的“向外传播”。 This way only the entries with matching painting entries should be left, and it should be sort of scalable. 这样,只有带有匹配绘画条目的条目才应保留,并且应该具有一定的可伸缩性。

The references aren't strictly necessary, one could add array key variables to the foreach loops instead. 引用不是严格必需的,可以将数组键变量添加到foreach循环中。 But I didn't like all the indexing this caused. 但是我不喜欢造成这种情况的所有索引。

$needle = 'sun';

foreach($arr as $entryKey => &$entry) {
    foreach($entry['children'] as $childKey => &$child) {
        foreach($child['painting'] as $paintingKey => $painting) {
            if (strpos($painting, $needle) === false) {
                unset($child['painting'][$paintingKey]);
            }
        }

        if (empty($child['painting'])) {
            unset($entry['children'][$childKey]);
        }
    }

    if (empty($entry['children'])) {
        unset($arr[$entryKey]);
    }
}
unset($entry, $child); // Take no risks with references

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM