简体   繁体   English

PHP:匹配任何包含字符串的数组过滤器

[英]PHP: array filter matching anything containing a string

I have a script which allows me to filter an entire multidimensional array according to a string.我有一个脚本,它允许我根据字符串过滤整个多维数组。 But it will only work if a value of the array matches the string exactly while I would like it to work when the string is at least contained in the value.但只有当数组的值与字符串完全匹配时它才会起作用,而我希望它在字符串至少包含在值中时起作用。

So, in other words, if the string is "nana", I would like the filtering to let the values "Banana" and "Ananas" be a match.所以,换句话说,如果字符串是“nana”,我希望过滤让值“Banana”和“Ananas”匹配。 So I would like the search to be done on "%nana%" in order to allow any combination of letters before and after the "nana" string.所以我希望在“%nana%”上进行搜索,以便允许在“nana”字符串之前和之后任意组合字母。

Here is my code so far:到目前为止,这是我的代码:

$dataset=[[3,"Yellow","Banana"], [2,"Brown","Ananas"], [1,"Green","brown"]];

$dataset = array_filter($dataset, function ($v){
    return filterArray('anana', $v);
});

function filterArray($needle,$haystack){
    $needle = strtolower($needle);

    foreach($haystack as $k => $v){
        $haystack[$k] = strtolower($v);
    };

    return in_array($needle,$haystack);
}

echo '<pre>'; print_r($dataset); echo '</pre>';

This won't work.这行不通。

I made my homework and found that "fnmatch" or "preg_match" are referenced rather often for this case.我做了功课,发现在这种情况下经常引用“fnmatch”或“preg_match”。 The problem is I don't see where they should be in my script.问题是我看不到它们应该在我的脚本中的位置。 The cases I've browsed are different enough for me not to know how to use them properly here.我浏览过的案例差异很大,以至于我不知道如何在这里正确使用它们。 So it's not about looking the proper function per se but rather about understanding how and where to use it.因此,这不是寻找正确的 function 本身,而是了解如何以及在何处使用它。

I tried this but it didn't work:我试过了,但没有用:

....
foreach($haystack as $k => $v){
    if(preg_match('/^'.$needle.'$/i', $v)) {    
        $haystack[$k] = strtolower($v);
    } 
};
....

And this didn't work neither:这也不起作用:

....
foreach($haystack as $k => $v){
    if(preg_match("/\b$needle\b/i", $v)) {
        $haystack[$k] = strtolower($v); 
    } 
};
....

I also tried this:我也试过这个:

....
$dataset = array_filter(dataset, function ($v){
    return filter((bool)preg_match("/$needle/i",$v);
});
....

None of those changes did any good and it feels to me like I exhausted the solutions found online and in here about this.这些更改都没有任何好处,我感觉就像我用尽了在线和这里找到的解决方案。

I also tried to use "fnmatch", even if I wasn't sure how and where to use it, but with no success.我也尝试使用“fnmatch”,即使我不确定如何以及在何处使用它,但没有成功。

So any help would be appreciated.所以任何帮助将不胜感激。

You can use stripos to do a case-insensitive search for the $needle anywhere inside the $haystack value:您可以使用stripos$haystack值内的任何位置的$needle进行不区分大小写的搜索:

$dataset=[[3,"Yellow","Banana"], [2,"Brown","Ananas"], [1,"Green","brown"]];

$dataset = array_filter($dataset, function ($v){
    return filterArray('anana', $v);
});

function filterArray($needle,$haystack){
    foreach($haystack as $v){
        if (stripos($v, $needle) !== false) return true;
    };
    return false;
}

echo '<pre>'; print_r($dataset); echo '</pre>';

Output: Output:

<pre>Array
(
    [0] => Array
        (
            [0] => 3
            [1] => Yellow
            [2] => Banana
        )
    [1] => Array
        (
            [0] => 2
            [1] => Brown
            [2] => Ananas
        )
)
</pre>

Demo on 3v4l.org 3v4l.org 上的演示

You may use stripos to check if a string contains another string.您可以使用stripos来检查一个字符串是否包含另一个字符串。

function filterArray($needle,$haystack){

    foreach($haystack as $k => $v){

        if (stripos($v, $needle) !== false) return true;

    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM