Моя цель - выполнить поиск в многомерном массиве с помощью Preg Match (просто КАК Operator Search в моем SQL). Я пробовал все возможные способы, которые знаю, но у меня ничего не работает. Мой текущий код поиска работает хорошо, но только с точным соответствием, я хочу, чтобы он выполнял поиск заголовков и категорий только из массива, чтобы он соответствовал pharse %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;
}
Вот демонстрационный массив 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/
)
)
Функция вызова
//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');