php preg_match() 期望参数 2 是字符串,给定数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33704065/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
preg_match() expects parameter 2 to be string, array given
提问by Christofer Hansen
array search is returning error.
数组搜索返回错误。
Suppose i have an arrays like so ---
假设我有一个像这样的数组 ---
[1] => Array
(
[id] => 11
[category] => phone cases
[country] => sweden
[sale_price] => 90,99
[price] => 120
[currency] => sek
[vat] => 19
[product_name] => "iphone 6 plus" case transparent
[description] => transparent case for iphone 6 plus
)
[2] => Array
(
[id] => 13
[category] => shoes
[country] => sweden
[sale_price] => 180,99
[price] => 200
[currency] => sek
[vat] => 19
[product_name] => blue platform shoes
Now i am trying to search something from this array, basically i am trying to find it from the $all_data, ['product_name']field
现在我试图从这个数组中搜索一些东西,基本上我试图从 $all_data, ['product_name']字段中找到它
$data = 'plus'; // what i want to search
$search = $this->my_array_search($all_data, $data);
function my_array_search($array, $string) {
$pattern = preg_replace('/\s+/', ' ', preg_quote($string));
return array_filter($array, function ($value) use($pattern) {
return preg_match('/' . $pattern . '/', $value) == 1;
});
}
but it is keep returning me an error --
但它不断向我返回一个错误-
preg_match() expects parameter 2 to be string, array given
preg_match() 期望参数 2 是字符串,给定数组
What i am doing wrong, do any one knows how to solve this problem !!
我做错了什么,有谁知道如何解决这个问题!!
采纳答案by bobble bubble
This is because you're filtering a two dimensional array, an array containing arrays.
这是因为您正在过滤一个二维数组,一个包含数组的数组。
The right regex function for searching an array would be preg_grep(). It returns an array of matches.
(don't forget to specify the delimiter with preg_quote
)
用于搜索数组的正确正则表达式函数是preg_grep()。它返回一个匹配数组。
(不要忘记用 指定分隔符preg_quote
)
$data = 'plus'; // what i want to search
$search = my_array_search($all_data, $data);
function my_array_search($array, $string)
{
$ret = false;
$pattern = preg_replace('/\s+/', ' ', preg_quote($string, '/'));
foreach($array AS $k => $v) {
$res = preg_grep('/' . $pattern . '/', $v);
if(!empty($res)) $ret[$k] = $res;
}
return $ret;
}
See demo at eval.in. Result array would consist of: key
=> array(matches)
请参阅 eval.in 中的演示。结果数组将包括:key
=>array(matches)
array(1) {
[1]=>
array(2) {
["product_name"]=>
string(32) ""iphone 6 plus" case transparent"
["description"]=>
string(34) "transparent case for iphone 6 plus"
}
}
To only search a certain column if PHP version >= 5.5 try using array_column.
如果 PHP 版本 >= 5.5 仅搜索某个列,请尝试使用array_column。
print_r(preg_grep('/plus/', array_column($all_data, 'product_name')));
Use i
modifierif you want to match case insensitive and add word boundariesif needed.
回答by Brandt Solovij
Something like this would do it - as someone pointed out, you have a nested array, one level deep.
像这样的事情可以做到 - 正如有人指出的那样,您有一个嵌套数组,深度为一层。
$data = 'plus'; // what i want to search
$search = $this->my_array_search($all_data, $data);
function my_array_search($array, $string) {
for ($i=0; $i< count($array); $i++)
{
$pattern = preg_replace('/\s+/', ' ', preg_quote($string));
return array_filter($array[$i], function ($value) use($pattern) {
return preg_match('/' . $pattern . '/', $value) == 1;
});
}
}
This can be improved upon to look into each value and check if it too is an array, and then recursed into - if thats something you're thinking could be useful in the future as well but you would need to work out how you are collecting matches in a better way
这可以改进以查看每个值并检查它是否也是一个数组,然后递归到 - 如果这是你认为将来也可能有用的东西,但你需要弄清楚你是如何收集的以更好的方式匹配