Q:

search words in two arrays

I am trying to search two arrays and return the index of matching words that match in array 1 from array 2. following are the arrays:
$array1 = array('hello how are you', 'hello I am fine');
$array2 = array('hello','how');

I am trying the following code and it return 0,1 which is fine. But i only want to return 0. I want it to return only where both words are present in the array. 
 foreach ($array1 as $reference => $array) {
    foreach($array2 as $key => $word) {
       if(strpos($array, $word) !== false) { 
           echo $reference, PHP_EOL;
           break;
       }    
    }    
 }

A:

You need to keep track of each entity from $array2 being checked against each entity from $array1 then compare after the inner loop to decide whether all elements from $array2 are present in $array1. Here's an example:
foreach($array1 as $reference => $array) {
    $contains = 0;
    foreach($array2 as $key => $word) {
        if(strpos($array, $word) !== false) {
            $contains++;
        } else {
            // for performance reasons, e.g. if you have a large array,
            // you should break the loop here if the word isn't in the
            // original array
            break;
        }
    }
    if($contains == count($array2)) {
        // $array contains all words from $array2
        echo $reference . PHP_EOL;
    } else {
        // $array doesn't contain all the words     
    }
}

