0

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.

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';

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.

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

Thanks!

0

3 Answers 3

2

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."
    }
}

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

0
1

You can first normalize your data and then use PHP's build in array functions to get the intersection between two arrays.

First of all convert each array with those multiple string with multiple words in there into an array only containing all words.

A helpful function to get all words from a string can be str_word_count.

Then compare those two "all words" arrays with each other using array_intersect.

Something like this:

$words1 =  array_unique(str_word_count(implode(' ', $Array), 1));
$words2 =  array_unique(str_word_count(implode(' ', $SecondArray), 1));
$intersection = array_intersect($words1, $words2);

if(count($intersection))
{
    # there is a match!
}
2
  • Hello, Thanks for the reply, i think i know what you mean here, although the problem is i need to keep it two seperate arrays as i need the index position for each array so i can store each arrays matched entries as variables for later use. Commented Oct 30, 2011 at 18:28
  • @ApiMail: You don't need to implode the arrays, so you can maintain indexes. If you add demo-data to your question and a bit better specify what you'd like to get from those, it should be trivial to change the code-example to better show you what I'm writing about. Commented Oct 30, 2011 at 23:16
0
function findUnit($packaging_units, $packaging)
{
    foreach ($packaging_units as $packaging_unit) {
        if (str_contains(strtoupper($packaging[3]), $packaging_unit)) {
            return $packaging_unit;
        }
    }
}

Here First parameter is array and second one is variable to find

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.