簡體   English   中英

PHP 使用 preg_match 進行多維數組搜索

[英]PHP Multidiamensional Array Search with preg_match

我的目標是使用 Preg Match 在多維數組中搜索(只是我的 SQL 中的 LIKE 運算符搜索),我嘗試了所有可能的方法,我知道但對我沒有任何效果。 我當前的搜索代碼效果很好,但只有完全匹配,我想只從數組中搜索標題和類別,以便它與短語%Action%匹配並提取可能的值或數組

這是我的 PHP 代碼

function searchMultiDimensionalArray($array, $key, $value) {
    $results = array();
    if (is_array($array)) {
        if (isset($array[$key]) && $array[$key] == $value)
            $results[] = $array;
        foreach ($array as $subarray)
            $results = array_merge($results, searchMultiDimensionalArray($subarray, $key, $value));
        }
    return $results;
 }

這是演示數組數組

(
    [rss] => Array
        (
            [channel] => Array
                (
                    [title] => Mobile Games TV
                    [atom:link] => 
                    [@atom:link] => Array
                        (
                            [href] => https://testdomain.com/feed/
                            [rel] => self
                            [type] => application/rss+xml
                        )

                    [link] => https://testdomain.com
                    [description] => Enjoy Free Games
                    [lastBuildDate] => Sun, 19 Apr 2020 18:51:20 +0000
                    [language] => en-US
                    [sy:updatePeriod] => 
    hourly  
                    [sy:updateFrequency] => 
    1   
                    [generator] => https://wordpress.org/?v=5.4.1
                    [item] => Array
                        (
                            [0] => Array
                                (
                                    [title] => Road Fury
                                    [link] => https://testdomain.com/puzzles/road-fury/
                                    [comments] => https://testdomain.com/puzzles/road-fury/#respond
                                    [dc:creator] => admin
                                    [pubDate] => Sun, 19 Apr 2020 18:51:20 +0000
                                    [category] => Array
                                        (
                                            [0] => Puzzles
                                            [1] => Car
                                            [2] => mobile
                                            [3] => Race
                                            [4] => Road
                                            [5] => Shoot
                                            [6] => Traffic
                                        )

                                    [guid] => https://testdomain.com/puzzles/road-fury/
                                    [@guid] => Array
                                        (
                                            [isPermaLink] => false
                                        )

                                    [description] => Unleash your fury on the highway an shoot down everything that moves! Tip: if you can’t shoot it, take it… those are power-ups! Upgrade your car to withstand a longer ride, beat bosses and get as far as you can so you can compare your highscores with others! Become the furious king of the road!Shoot […]
                                    [content:encoded] => 
Unleash your fury on the highway an shoot down everything that moves! Tip: if you can’t shoot it, take it… those are power-ups! Upgrade your car to withstand a longer ride, beat bosses and get as far as you can so you can compare your highscores with others! Become the furious king of the road!
Shoot down as many cars as you can and collect power-ups and cash for upgrades Enemy cars have different abilities and they drop different stuff find out what is possible 



                                    [wfw:commentRss] => https://testdomain.com/puzzles/road-fury/feed/
                                    [slash:comments] => 0
                                )

                            [1] => Array
                                (
                                    [title] => Tasty Jewel
                                    [link] => https://testdomain.com/puzzles/tasty-jewel/
                                    [comments] => https://testdomain.com/puzzles/tasty-jewel/#respond
                                    [dc:creator] => admin
                                    [pubDate] => Sun, 19 Apr 2020 18:51:17 +0000
                                    [category] => Array
                                        (
                                            [0] => Puzzles
                                            [1] => Arcade
                                            [2] => Bomb
                                            [3] => Candy
                                            [4] => Jewel
                                            [5] => Logic
                                            [6] => Match 3
                                            [7] => Match3
                                            [8] => mobile
                                        )

                                    [guid] => https://testdomain.com/puzzles/tasty-jewel/
                                    [@guid] => Array
                                        (
                                            [isPermaLink] => false
                                        )

                                    [description] => This box of sweets contains precious jewel candies! Match and combine them to craft exploding ones! It can help you crush through blocks standing in your way. The classic turn based match-3 arcade with lots of challenging levels and modes awaits you!Swap and match the jewel sweets to form a chain of 3 or more […]
                                    [content:encoded] => 
This box of sweets contains precious jewel candies! Match and combine them to craft exploding ones! It can help you crush through blocks standing in your way. The classic turn based match-3 arcade with lots of challenging levels and modes awaits you!
Swap and match the jewel sweets to form a chain of 3 or more of the same colour You will be rewarded with explosive power-ups if you match 4 or more 



                                    [wfw:commentRss] => https://testdomain.com/puzzles/tasty-jewel/feed/
                                    [slash:comments] => 0
                                )
    [@rss] => Array
        (
            [version] => 2.0
            [xmlns:content] => http://purl.org/rss/1.0/modules/content/
            [xmlns:wfw] => http://wellformedweb.org/CommentAPI/
            [xmlns:dc] => http://purl.org/dc/elements/1.1/
            [xmlns:atom] => http://www.w3.org/2005/Atom
            [xmlns:sy] => http://purl.org/rss/1.0/modules/syndication/
            [xmlns:slash] => http://purl.org/rss/1.0/modules/slash/
        )

)

調用 function

//Find title with extact match
$finder_title = searchMultiDimensionalArray($array_data, 'title', 'Road Fury');
//Find category which isn't working
$find_cat = searchMultiDimensionalArray($array_data, 'category', 'Action');

您好,使用函數式編程方法查看此解決方案:

<?php

function searchMultiDimensionalArray($array, $key, $value) {

    if(!is_array($array)) {
        return preg_match("/$value/i", $array);
    }

    return array_filter($array, function($item) use ($key, $value){
       return  (isset($item[$key]) && !is_array($item[$key]) && preg_match("/$value/i", $item[$key])) 
       || searchMultiDimensionalArray($item, $key, $value);
    });
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM