要查找符合搜索条件的值,可以使用
array_filter函数:
$example = array('An example','Another example','Last example');$searchword = 'last';$matches = array_filter($example, function($var) use ($searchword) { return preg_match("/b$searchwordb/i", $var); });现在,
$matches数组将仅包含原始数组中包含单词 last (不区分大小写)的元素。
如果需要查找与条件匹配的值的键,则需要遍历数组:
$example = array('An example','Another example','One Example','Last example');$searchword = 'last';$matches = array();foreach($example as $k=>$v) { if(preg_match("/b$searchwordb/i", $v)) { $matches[$k] = $v; }}现在,数组
$matches包含原始数组中的键/值对,其中值包含(不区分大小写)单词 last 。



