簡體   English   中英

在PHP中的多維數組中搜索過濾器

[英]Search filter in multidimensional array in php

如何創建在多維嵌套數組中搜索的搜索功能?

給定以下數組:

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
                            )

                    )

            )

    )
)

如何在“繪畫”中使用搜索關鍵字過濾此數組?

例如,搜索關鍵字為“ sun”

結果應該是這樣的:

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

如果對array_filter()使用回調,則可以進行相當高級的過濾。

編輯新版本的答案,以刪除繪畫陣列中與模式不匹配的條目。

通過執行嵌套的foreach循環(一直到所需的最里面的數組)進行工作,在這種情況下進行繪畫 繪畫數組被過濾,以便刪除其中所有不匹配的元素。 完成此操作后,在下一個向外的級別檢查繪畫數組,如果為空,則除去類型的“向外傳播”。 這樣,只有帶有匹配繪畫條目的條目才應保留,並且應該具有一定的可伸縮性。

引用不是嚴格必需的,可以將數組鍵變量添加到foreach循環中。 但是我不喜歡造成這種情況的所有索引。

$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