比较数组中的所有字符串到另一个数组中的所有字符串PHP [英] Compare All strings in a array to all strings in another array, PHP

查看:196
本文介绍了比较数组中的所有字符串到另一个数组中的所有字符串PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做的是真的,但我会进入很多细节,以确保它是容易理解。
我有一个数组有几个字符串。然后我有另一个有很少的其他短字符串在它通常一个或两个字。

What i am trying to do is really but i am going into a lot of detail to make sure it is easily understandable. I have a array that has a few strings in it. I then have another that has few other short strings in it usually one or two words.

我需要它,所以如果我的应用程序发现其中一个字符串中的第二数组,在第一个数组字符串之一,它将继续下一个动作。
例如,如果第一个数组中的一个字符串是This is PHP Code,然后第二个字符串中的一个是PHP。然后它找到一个匹配,继续下一个动作。我可以使用这个代码:

I need it so that if my app finds one of the string words in the second array, in one of the first arrays string it will proceed to the next action. So for example if one of the strings in the first array is "This is PHP Code" and then one of the strings in the second is "PHP" Then it finds a match it proceeds to the next action. I can do this using this code:

for ( $i = 0; $i < count($Array); $i++) {
    $Arrays = strpos($Array[$i],$SecondArray[$i]);

    if ($Arrays === false) {

        echo 'Not Found Array String';

    }
    else {
        echo 'Found Array String';

但是,这只会将循环中当前索引处的First Array对象与第二个Array对象current

However this only compares the First Array object at the current index in the loop with the Second Array objects current index in the loop.

我需要它来比较数组中的所有值,以便它在第一个数组中搜索第一个数组中的每个值, ,然后第一个数组中的每个值在第二个数组中的第二个值,等等。

I need it to compare all the values in the array, so that it searches every value in the first array for the First Value in the second array, then every value in the First array for the Second value in the second array and so on.

我想我必须做两个循环?我试过这个,但有问题的数组只返回第一个值。

I think i have to do two loops? I tried this but had problems with the array only returning the first value.

如果任何人都可以帮助它将不胜感激!
Ill标记正确答案和+1任何有用的注释。

If anyone could help it would be appreciated! Ill mark the correct answer and + 1 any helpful comments!

谢谢!

推荐答案

也许以下是一个解决方案:

Maybe the following is a solution:

// loop through array1
foreach($array1 as $line) {
    // check if the word is found
    $word_found = false;

    // explode on every word
    $words = explode(" ", $line);

    // loop through every word
    foreach($words as $word) {
        if(in_array($word, $array2)) {
            $word_found = true;
            break;
        }
    }

    // if the word is found do something
    if($word_found) {
        echo "There is a match found.";
    } else {
        echo "No match found."
    }
}

应该给你想要的结果。我绝对确定有一个更有效的方法来做这个..但那是你的2找出我quess ..好运气

Should give you the result you want. I'm absolute sure there is a more efficient way to do this.. but thats for you 2 find out i quess.. good luck

这篇关于比较数组中的所有字符串到另一个数组中的所有字符串PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆