0

我的问题与 PHP 有关

我有 2 个数组:

Array1 
(
    [0] => "Pecan, Blackberry, Peach, Apple, Orange, Banana"
    [1] => "Potato, Tomato, Broccoli, Spinach"
    [2] => "Cake, Ice-cream, Candy, Jelly, Chocolate"
}

Array2 
(
    [0] => "Banana"
    [1] => "Apple"
    [2] => "Peach"
}

我只想匹配Array2[0]元素Array1[0]以检查Array2[0](在这种情况下,它是)的值是否Banana存在于元素中Array1[0]

虽然,我可以通过某种方式解决这个问题,但我想知道是否有快速、内存消耗更少的内置函数或其他方式,因为我需要在页面加载时执行 10 次。

4

2 回答 2

1

如果我正确理解你的问题,这应该是你所追求的:

foreach ($array2 as $key => val) {
    if (stripos($array1[$key], $val) !== false) {
        // match
    }
}
于 2012-09-26T11:07:00.667 回答
0

这将找到确切的值(Bananaaaaa 与 Banana 不同)。

代码

foreach ($array2 as $key => $val) {
    if (in_array($val, explode(', ', $array1[$key]))) {
        var_dump("$val is found in \$array1[$key]");
    } else {
        var_dump("$val is not found in \$array1[$key]");
    }
}

输出

string(29) "Banana is found in $array1[0]"
string(32) "Apple is not found in $array1[1]"
string(32) "Peach is not found in $array1[2]"
于 2012-09-26T11:15:33.907 回答