【发布时间】:2011-10-19 08:49:20
【问题描述】:
我有一个数组解析函数,用于查找值中的部分单词匹配。如何使其递归以使其适用于多维数组?
function array_find($needle, array $haystack)
{
foreach ($haystack as $key => $value) {
if (false !== stripos($needle, $value)) {
return $key;
}
}
return false;
}
我需要搜索的数组
array(
[0] =>
array(
['text'] =>'some text A'
['id'] =>'some int 1'
)
[1] =>
array(
['text'] =>'some text B'
['id'] =>'some int 2'
)
[2] =>
array(
['text'] =>'some text C'
['id'] =>'some int 3'
)
[3] =>
array(
['text'] =>'some text D'
['id'] =>'some int 4'
)
etc..
【问题讨论】:
-
你可以使用RecursiveFilterIterator的实现。
-
这个问题有帮助吗? stackoverflow.com/questions/2504685/…
-
@afuzzyllama,不,我有钥匙。我需要检查任何值中是否存在部分单词匹配,然后取回关联键 => 值。这会找到整个值匹配,然后为您提供我并不真正需要的父键。
标签: php search recursion multidimensional-array