【问题标题】:preg_match() expects parameter 2 to be string, array givenpreg_match() 期望参数 2 是字符串,给定数组
【发布时间】:2015-11-14 01:08:06
【问题描述】:

数组搜索返回错误。

假设我有一个这样的数组 ---

[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

现在我试图从这个数组中搜索一些东西,基本上我试图从 $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;
    });
}

但它一直给我一个错误--

preg_match() 期望参数 2 是字符串,给定数组

我做错了什么,有谁知道如何解决这个问题!!

【问题讨论】:

  • 顶级数组的项目是数组,而不是字符串。
  • @MaratTanalin 我应该如何从数组中搜索
  • $value 是一个数组,$value['product_name'] 是该数组中您似乎感兴趣的字符串。
  • @Sammitch 是的,我应该如何从['product_name'] 字段中搜索!

标签: php arrays regex


【解决方案1】:

这是因为您正在过滤 two dimensional array,这是一个包含数组的数组。

搜索数组的正确正则表达式函数是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。结果数组将包括: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"
  }
}

如果 PHP 版本 >= 5.5 仅搜索特定列,请尝试使用array_column

print_r(preg_grep('/plus/', array_column($all_data, 'product_name')));

如果您想匹配不区分大小写,请使用i modifier,如果需要,请添加word boundaries

【讨论】:

    【解决方案2】:

    这样的事情可以做到 - 正如有人指出的那样,你有一个嵌套数组,一层深。

    $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;
            });
        }
    
    }
    

    这可以改进以查看每个值并检查它是否也是一个数组,然后递归到 - 如果您认为这在未来也可能有用,但您需要弄清楚如何您正在以更好的方式收集比赛

    【讨论】:

      猜你喜欢
      • 2017-01-02
      • 2023-03-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多