0

My goal is to search within multi diamensional array using Preg Match (just LIKE Operator Search in my SQL), I tried all the possible ways which I know but nothing works for me. My current search code works well but only with the exact match, I want to do it search titles and categories only from the array so that it will matches with the pharse %Action% and extract the possible value or array

Here is my PHP code

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

Here is the demo Array Array

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

)

Calling 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');

1 Answer 1

1

Hello look this solution using functional programming approach:

<?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);
    });
}
Sign up to request clarification or add additional context in comments.

1 Comment

Sorry, I've updated the function to search by category too now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.