在PHP中使用常规的前pressions搜索多维数组和返回键 [英] Search multidimensional array using regular expressions in PHP and return keys
问题描述
此问题涉及到以下问题:
<一href=\"http://stackoverflow.com/questions/22345991/search-multidimensional-array-in-php-and-return-keys\">Search在PHP多维数组和返回键
This issue is related to following issue: Search multidimensional array in PHP and return keys
我有以下阵列我想搜索的字符串使用例如常规的前pressions
I have following array I would like to search for strings using e.g. regular expressions:
[390] => Array
(
[0] => hammer
[1] => Properties
[2] => tools, hammer, properties
[3] =>
[4] => done
[png] => Array
(
[0] => hammer_16x.png
[1] => hammer_32x.png
)
[eps] => Array
(
[0] => hammer_16x.eps
[1] => hammer_32x.eps
)
[ico] => Array
(
[0] => hammer.ico
)
)
我想特别是搜索这些值:
I would like to especially search these values:
[0] => hammer
[1] => Properties
[2] => tools, hammer, properties
[3] =>
[4] => done
例如。为火腿进行搜索时,用户必须要找到这个数组键的可能性,工具,AMM等。
e.g. the user shall have the possibility to find this array key when searching for "ham", "tools", "amm" etc.
我试着去适应张贴在上面后的解决方案,但并没有对其进行管理。我还发现一个解决方案使用array_map,但这并没有使我在一个特定的属性明确搜索(例如:我想进一步对限制搜索数组中的第一个索引,这里[0] =>锤)
I tried to adapt the solution posted in the post above but did not manage it. I've also found a solution using array_map, but this did not enable me to explicitly search in a specific attribute (e.g. I would like to further on limit a search to the first index in the array, here [0] => hammer):
$result= array_map('unserialize', preg_filter('/'.$searchterm.'/', '$0', array_map('serialize', $array)));
您的想法是欢迎的。
谢谢!
Your ideas are welcome :) Thanks!
推荐答案
您可以使用的 preg_grep :
function preg_grep_recursive( $pattern, $input, $flags = 0, &$output = array() )
{
foreach($input as $key => $val) {
if(is_array($val)) {
preg_grep_recursive($pattern, $val, $flags, $output);
} else {
$output[] = $val;
}
}
return preg_grep($pattern, $output, $flags);
}
这篇关于在PHP中使用常规的前pressions搜索多维数组和返回键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!