繁体   English   中英

PHP 检查字符串数组是否包含子字符串数组中的任何值

[英]PHP Check if an array of strings contains any value from an array of substrings

例如。 我有 2 个 arrays:

$arr1 = array("cake", "make"); $arr2 = array("birthday cake", "maker", "a random index");

我想检查$arr1的两个值是否作为 substring 存在于$arr2中。 $arr2也可能有其他元素,但它是否包含$arr1的所有索引作为子字符串并不重要。

我知道标准的嵌套循环方法,但我不想使用它,因为性能会很糟糕(因为我有一个大数组)。 我想问一下 PHP 是否包含内置的 function ,或者是否有另一种方法可以以更好的时间复杂度完成此任务[最好是 O(log n),但 O(n) 也可以]。

如果你说你有一个大数组要检查,优化 arrays 的处理。

  1. 一旦您在 arr1 中找不到其中一项,它就会全部终止并返回 false
  2. 通过引用传递数组参数,因此无需将 arrays 复制到堆栈
  3. 将最有可能在 arr2 中找不到的东西放在 arr1 的开头
$arr1 = array( "cake", "make");
$arr2 = array("birthday cake", "maker", "a random index");

// pass arrays by reference, so you dont have to create copies 
// of large arrays on to the stack
function testThem(&$arr1, &$arr2) {
    // as soon as a unfound situation is indicated, return from the function with false
    foreach ($arr1 as $a1) {
        $found = false;
        foreach ($arr2 as $a2) {
            if (strpos($a2, $a1) !== false) {
                //as soon as a found occurs break out of this foreach
                $found = true;
                break;
            }
        }
        // If we get here with a $found still = false
        // we didnt find one of the things in $arr1 within $arr2 
        // so no more looping required. Finish and return false
        if (!$found) { 
            return false;
        }
    }    
    // only if we find all the items do we eventually get here after lots of loops
    return true;
}


if ( testThem($arr1, $arr2) ) {
    echo 'All Found';
} else {
    echo 'Something not found';
}

如果我们制作第二个数组的字符串

$arr2 = implode('###',$arr2);

然后使用 array_filter 如下:

$result = array_filter($arr1,function($value) use($arr2){
    return (strpos($value,$arr2)!==false)?$value:false;
});

print_r(array_filter($result));

这里我们有来自 $arr1 的所有匹配子字符串。

我认为嵌套循环是唯一的方法,但是一旦确定答案,就可以更有效地跳出循环。

$found_all = true;
foreach ($arr1 as $str1) {
    $found1 = false;
    foreach ($arr2 as $str2) {
        if (strpos($str2, $str1) !== false) {
            $found1 = true;
            break;
        }
    }
    if (!$found1) {
        $found_all = false;
        break;
    }
}
echo $found_all ? "All found" : "Not all found";

暂无
暂无

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

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