通过搜索键和值中的部分匹配来过滤数组元素
Filter array elements by searching for partial match in keys and values
是否可以像在 MySQL.
中那样在 PHP 数组中进行搜索
例如:我有这个array
array(
'mark@test.com'=> `Mark Mian`,
'jhon@test.com'=> `John jack`,
'logon@test.com'=> `Bob Logon`,
'Stela@test.com'=> `Stela Josh`,
'json@test.com'=> `Json Josh`
'bobby@test.com'=> `Bob Mark`
)
我会做这种类型的搜索,
例如:如果我搜索 Mark
它应该 return 我这个
'mark@test.com' => `Mark Mian
如果我搜索 Bob
它应该 return 我
'bobby@test.com'=> Bob Mark
'logon@test.com'=> Bob Logon
,
如果我只搜索 a
它应该 return 我那些包含 a
的元素 例如:
'mark@test.com'=> Mark Mian
,
'jhon@test.com'=> John jack
,
'Stela@test.com'=> Stela Josh
,
'bobby@test.com'=> Bob Mark
注意:搜索应该是按键或值
$needle="bob";
$output=array();
foreach($array as $k=>$v)
{
if(stristr($k,$needle) || stristr($v,$needle))
$output[$k]=$v;
}
print_r($output);
也就是说,如果您想同时搜索键和值,如果您只想搜索值,请删除 keys 部分。
一个简单的方法是使用 array_filter
如果你想要正则表达式,这行得通
$regex = '~test~';
$result = array_filter($data, function($item) use ($regex) {
return preg_match($regex, $item);
});
或者只是一个简单的包含搜索
$search = 'test';
$result = array_filter($data, function($item) use ($search) {
return stristr($value, $search);
});
如果您必须同时搜索键和值,您可以将参数 ARRAY_FILTER_USE_BOTH
附加到 array_filter。
$search = 'test';
$result = array_filter($data, function($item, $key) use ($search) {
return stristr($value, $search) || stristr($key, $search);
}, ARRAY_FILTER_USE_BOTH);
最后,您可以将 array_filter 与 preg_grep 组合起来同时搜索两者。
$search = '~bob~i';
$result = array_filter($data, function() use ($search) {
return count(preg_grep($search, func_get_args()));
}, ARRAY_FILTER_USE_BOTH);
这是一个 preg_grep
解决方案,应该更像 MySQL 中的 WHERE REGEXP 'PATTERN'
。我修改了 Daniel Klein's preg_grep_keys
function to search for the pattern within array keys, and added an array_merge
to it, which should work with the arrays with non-numeric keys. If the keys are numeric, just use a mere preg_grep
solution(preg_grep('~Mark~i', $arr);
以查找具有 mark
或 Mark
等的所有数组元素)。
array_merge
Merges the elements of one or more arrays together so that the values of one are appended to the end of the previous one. It returns the resulting array.
If the input arrays have the same string keys, then the later value for that key will overwrite the previous one. If, however, the arrays contain numeric keys, the later value will not overwrite the original value, but will be appended.
function preg_grep_keys_values($pattern, $input, $flags = 0) {
return array_merge(
array_intersect_key($input, array_flip(preg_grep($pattern, array_keys($input), $flags))),
preg_grep($pattern, $input, $flags)
);
}
$a = array(
'mark@test.by.com'=> "Mark Mian lv",
'jhon@test.lv.com'=> "John jack lv",
'logon@test.en.com'=> "Bob Logon",
'Stela@test.es.com'=> "Stela Josh",
'json@test.es.com'=> "Json Josh",
'bobby@test.lv.com'=> "Bob Mark"
);
$r = preg_grep_keys_values('~lv~i', $a);
print_r($r);
上面的代码首先在键中搜索 lv
(不区分大小写),然后在值中搜索,然后将结果合并到 1 个数组中。因此,结果是:
[jhon@test.lv.com] => John jack lv
[bobby@test.lv.com] => Bob Mark
[mark@test.by.com] => Mark Mian lv
你要过滤一个数组,用array_filter
就是专为。
如果您只搜索文字字符串,则不需要使用正则表达式:
$needle = 'bob';
$result = array_filter($data, function ($k, $v) use ($needle) {
return stripos($k, $needle) !== false || stripos($v, $needle) !== false;
}, ARRAY_FILTER_USE_BOTH);
如果您希望能够使用正则表达式进行过滤:
$pattern = '~n.*e~i';
$result = array_filter($data, function ($k, $v) use ($pattern) {
return !empty(preg_grep($pattern, [$k, $v]));
}, ARRAY_FILTER_USE_BOTH);
$search = "Mark"
$array = array(
'mark@test.com'=> `Mark Mian`,
'jhon@test.com'=> `John jack`,
'logon@test.com'=> `Bob Logon`,
'Stela@test.com'=> `Stela Josh`,
'json@test.com'=> `Json Josh`
'bobby@test.com'=> `Bob Mark`
)
foreach ($array as $key => $value) {
if (stristr($value, $search) == '') {
//not found
}else{
//found
}
这是搜索任何子字符串的最佳方法,不区分大小写且快速
just like like in mysql
例如:
select * from table where name = "%Mark%"
从 PHP8 开始,str_contain()
成为在不减少语法的情况下进行不区分大小写搜索的首选本机函数。
搜索小写 b
很好地演示了对键和值的不区分大小写的搜索。
代码:(Demo)
$array = [
'mark@test.by.com'=> "Mark Mian lv",
'jhon@test.lv.com'=> "John jack lv",
'logon@test.en.com'=> "Bob Logon",
'Stela@test.es.com'=> "Stela Josh",
'json@test.es.com'=> "Json Josh",
'bobby@test.lv.com'=> "Bob Mark"
];
$needle = 'b';
var_export(
array_filter(
$array,
fn($v, $k) => str_contains($v, $needle) || str_contains($k, $needle),
ARRAY_FILTER_USE_BOTH
)
);
输出:
array (
'mark@test.by.com' => 'Mark Mian lv',
'logon@test.en.com' => 'Bob Logon',
'bobby@test.lv.com' => 'Bob Mark',
)
如果你想变得可爱并在 array_filter()
中进行单个函数调用,那么你可以将 $v
和 $k
连接在一起,但是你冒着任意指定的风险匹配的定界字符。为了稳定性,每次迭代只调用两次。
是否可以像在 MySQL.
中那样在 PHP 数组中进行搜索例如:我有这个array
array(
'mark@test.com'=> `Mark Mian`,
'jhon@test.com'=> `John jack`,
'logon@test.com'=> `Bob Logon`,
'Stela@test.com'=> `Stela Josh`,
'json@test.com'=> `Json Josh`
'bobby@test.com'=> `Bob Mark`
)
我会做这种类型的搜索,
例如:如果我搜索 Mark
它应该 return 我这个
'mark@test.com' => `Mark Mian
如果我搜索 Bob
它应该 return 我
'bobby@test.com'=>
Bob Mark
'logon@test.com'=>
Bob Logon
,
如果我只搜索 a
它应该 return 我那些包含 a
的元素 例如:
'mark@test.com'=>
Mark Mian
,'jhon@test.com'=>
John jack
,'Stela@test.com'=>
Stela Josh
,'bobby@test.com'=>
Bob Mark
注意:搜索应该是按键或值
$needle="bob";
$output=array();
foreach($array as $k=>$v)
{
if(stristr($k,$needle) || stristr($v,$needle))
$output[$k]=$v;
}
print_r($output);
也就是说,如果您想同时搜索键和值,如果您只想搜索值,请删除 keys 部分。
一个简单的方法是使用 array_filter
如果你想要正则表达式,这行得通
$regex = '~test~';
$result = array_filter($data, function($item) use ($regex) {
return preg_match($regex, $item);
});
或者只是一个简单的包含搜索
$search = 'test';
$result = array_filter($data, function($item) use ($search) {
return stristr($value, $search);
});
如果您必须同时搜索键和值,您可以将参数 ARRAY_FILTER_USE_BOTH
附加到 array_filter。
$search = 'test';
$result = array_filter($data, function($item, $key) use ($search) {
return stristr($value, $search) || stristr($key, $search);
}, ARRAY_FILTER_USE_BOTH);
最后,您可以将 array_filter 与 preg_grep 组合起来同时搜索两者。
$search = '~bob~i';
$result = array_filter($data, function() use ($search) {
return count(preg_grep($search, func_get_args()));
}, ARRAY_FILTER_USE_BOTH);
这是一个 preg_grep
解决方案,应该更像 MySQL 中的 WHERE REGEXP 'PATTERN'
。我修改了 Daniel Klein's preg_grep_keys
function to search for the pattern within array keys, and added an array_merge
to it, which should work with the arrays with non-numeric keys. If the keys are numeric, just use a mere preg_grep
solution(preg_grep('~Mark~i', $arr);
以查找具有 mark
或 Mark
等的所有数组元素)。
array_merge
Merges the elements of one or more arrays together so that the values of one are appended to the end of the previous one. It returns the resulting array. If the input arrays have the same string keys, then the later value for that key will overwrite the previous one. If, however, the arrays contain numeric keys, the later value will not overwrite the original value, but will be appended.
function preg_grep_keys_values($pattern, $input, $flags = 0) {
return array_merge(
array_intersect_key($input, array_flip(preg_grep($pattern, array_keys($input), $flags))),
preg_grep($pattern, $input, $flags)
);
}
$a = array(
'mark@test.by.com'=> "Mark Mian lv",
'jhon@test.lv.com'=> "John jack lv",
'logon@test.en.com'=> "Bob Logon",
'Stela@test.es.com'=> "Stela Josh",
'json@test.es.com'=> "Json Josh",
'bobby@test.lv.com'=> "Bob Mark"
);
$r = preg_grep_keys_values('~lv~i', $a);
print_r($r);
上面的代码首先在键中搜索 lv
(不区分大小写),然后在值中搜索,然后将结果合并到 1 个数组中。因此,结果是:
[jhon@test.lv.com] => John jack lv
[bobby@test.lv.com] => Bob Mark
[mark@test.by.com] => Mark Mian lv
你要过滤一个数组,用array_filter
就是专为。
如果您只搜索文字字符串,则不需要使用正则表达式:
$needle = 'bob';
$result = array_filter($data, function ($k, $v) use ($needle) {
return stripos($k, $needle) !== false || stripos($v, $needle) !== false;
}, ARRAY_FILTER_USE_BOTH);
如果您希望能够使用正则表达式进行过滤:
$pattern = '~n.*e~i';
$result = array_filter($data, function ($k, $v) use ($pattern) {
return !empty(preg_grep($pattern, [$k, $v]));
}, ARRAY_FILTER_USE_BOTH);
$search = "Mark"
$array = array(
'mark@test.com'=> `Mark Mian`,
'jhon@test.com'=> `John jack`,
'logon@test.com'=> `Bob Logon`,
'Stela@test.com'=> `Stela Josh`,
'json@test.com'=> `Json Josh`
'bobby@test.com'=> `Bob Mark`
)
foreach ($array as $key => $value) {
if (stristr($value, $search) == '') {
//not found
}else{
//found
}
这是搜索任何子字符串的最佳方法,不区分大小写且快速
just like like in mysql
例如:
select * from table where name = "%Mark%"
从 PHP8 开始,str_contain()
成为在不减少语法的情况下进行不区分大小写搜索的首选本机函数。
搜索小写 b
很好地演示了对键和值的不区分大小写的搜索。
代码:(Demo)
$array = [
'mark@test.by.com'=> "Mark Mian lv",
'jhon@test.lv.com'=> "John jack lv",
'logon@test.en.com'=> "Bob Logon",
'Stela@test.es.com'=> "Stela Josh",
'json@test.es.com'=> "Json Josh",
'bobby@test.lv.com'=> "Bob Mark"
];
$needle = 'b';
var_export(
array_filter(
$array,
fn($v, $k) => str_contains($v, $needle) || str_contains($k, $needle),
ARRAY_FILTER_USE_BOTH
)
);
输出:
array (
'mark@test.by.com' => 'Mark Mian lv',
'logon@test.en.com' => 'Bob Logon',
'bobby@test.lv.com' => 'Bob Mark',
)
如果你想变得可爱并在 array_filter()
中进行单个函数调用,那么你可以将 $v
和 $k
连接在一起,但是你冒着任意指定的风险匹配的定界字符。为了稳定性,每次迭代只调用两次。