我如何过滤数组并匹配 return
how can i filter an array and return matches
我想要 return 项 "like" 其他东西。
我试过array_filter,但不能正确使用。
这是我试过的。
期望的输出是
one.php2000565, one.php999.php .
Array([0] =>
one.php2000565[1]
=> two.php[2] => three.php[3] => one.php999.php[4] => four.php)
$search_text = 'one.php';
array_filter($array, function($a) use ($search_text) {
return ( strpos($a, $search_text) !== false );
});
Array([0] => one.php2000565[1] => two.php[2] => three.php[3] => one.php999.php[4] => four.php)
$search_text = 'one.php';
array_filter($array, function($a) use ($search_text) {
return ( strpos($a, $search_text) !== false );
});
你可以试试array_filter:
$search_text = 'one.php';
array_filter($yourArray, function($el) use ($search_text) {
return ( strpos($el, $search_text) !== false );
});
$res = array_filter($files, function($files) use ($search_program) {
return ( strpos($files, $search_program) !== false );
});
print_r($res);
您可以尝试以下解决方案:-
$example = array([0] => one.php2000565[1] => two.php[2] => three.php[3] => one.php999.php[4] => four.php);
$searchword = 'one.php';
$matches = array_filter($example, function($var) use ($searchword) { return preg_match("/\b$searchword\b/i", $var); });
可能对你有帮助。
您没有分配 array_filter
的结果。 PHP 的 array_filter
returns修改后的数组。所以只需使用:
$array = array_filter($array,
function($a) use ($search_text) {
return (
strpos($a, $search_text) !== false
);
}
)
正如我评论的那样,OP 代码在 array_filter
下工作正常,只需要在变量中分配过滤后的值。但是我的解决方案是 array_filter
的替代方案,您可以像
一样使用 preg_grep
$res = preg_grep("/$search_text/",$array);
print_r($res);
我想要 return 项 "like" 其他东西。 我试过array_filter,但不能正确使用。
这是我试过的。 期望的输出是
one.php2000565, one.php999.php . Array([0] => one.php2000565[1] => two.php[2] => three.php[3] => one.php999.php[4] => four.php)
$search_text = 'one.php';
array_filter($array, function($a) use ($search_text) {
return ( strpos($a, $search_text) !== false );
});
Array([0] => one.php2000565[1] => two.php[2] => three.php[3] => one.php999.php[4] => four.php)
$search_text = 'one.php';
array_filter($array, function($a) use ($search_text) {
return ( strpos($a, $search_text) !== false );
});
你可以试试array_filter:
$search_text = 'one.php';
array_filter($yourArray, function($el) use ($search_text) {
return ( strpos($el, $search_text) !== false );
});
$res = array_filter($files, function($files) use ($search_program) {
return ( strpos($files, $search_program) !== false );
});
print_r($res);
您可以尝试以下解决方案:-
$example = array([0] => one.php2000565[1] => two.php[2] => three.php[3] => one.php999.php[4] => four.php);
$searchword = 'one.php';
$matches = array_filter($example, function($var) use ($searchword) { return preg_match("/\b$searchword\b/i", $var); });
可能对你有帮助。
您没有分配 array_filter
的结果。 PHP 的 array_filter
returns修改后的数组。所以只需使用:
$array = array_filter($array,
function($a) use ($search_text) {
return (
strpos($a, $search_text) !== false
);
}
)
正如我评论的那样,OP 代码在 array_filter
下工作正常,只需要在变量中分配过滤后的值。但是我的解决方案是 array_filter
的替代方案,您可以像
preg_grep
$res = preg_grep("/$search_text/",$array);
print_r($res);