我的目标是使用Preg Match在多维数组中进行搜索(就像我的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;
}这是一个演示数组
(
[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');发布于 2020-05-07 03:08:09
你好,看看这个使用函数式编程方法的解决方案:
<?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);
});
}https://stackoverflow.com/questions/61641329
复制相似问题