How to search a multidimensional array using multiple terms
<p>Hey guys i've got a basic test file search engine here, all finished (up to the standard that is required for the project) apart from being able to search for multiple words. Im just wondering if this is possible the way ive done it?</p>

<p>Heres what ive done:</p>

<pre><code>&lt;?php
// Calculate the start time of page loading
$starttime = microtime();
$startarray = explode(" ", $starttime);
$starttime = $startarray[1] + $startarray[0];

// Check to see if a search word has been submitted
if(isset($_GET['search']))
{
    $searchWord = $_GET['search'];
}
else
{
    $searchWord = null;
}

?&gt;
&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
&lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
&lt;head&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /&gt;
&lt;title&gt;Untitled Document&lt;/title&gt;
&lt;link href="style.css" rel="stylesheet" type="text/css" /&gt;
&lt;/head&gt;

&lt;body&gt;
&lt;div id="wrapper"&gt;
    &lt;div id="searchbar"&gt;
        &lt;h1&gt;PHP Search&lt;/h1&gt;
        &lt;form name='searchform' id='searchform' action='&lt;?php echo $_SERVER['PHP_SELF']; ?&gt;' method='get'&gt;
          &lt;input type='text' name='search' id='search' value='&lt;?php echo $_GET['search']; ?&gt;' /&gt;
            &lt;input type='submit' value='Search' /&gt;
        &lt;/form&gt;
        &lt;br /&gt;
        &lt;br /&gt;
    &lt;/div&gt;&lt;!-- close searchbar --&gt;
&lt;?php
// Includes external php file that holds list of common/stop words
include "commonwords.php";
// Initialise index array
$index = array();
// Initialise words array
$words = array();
// Initialise number of docs (in directory) array
$num_docs = 0;

// All files with a .txt extension
foreach (glob("./files/*.txt") as $filename) {

    // Includes the file based on the include_path
    $content = file_get_contents($filename, true);

    // Used to replace all illegal characters and spaces etc
    $pat[0] = "/^\s+/";
    $pat[1] = "/\s{2,}/";
    $pat[2] = "/\s+\$/";
    $rep[0] = "";
    $rep[1] = " ";
    $rep[2] = "";

    $new_content = preg_replace("/[^A-Za-z0-9\s\s+]/", "", $content);
    $new_content = preg_replace($pat, $rep, $new_content);
    $new_content = strtolower($new_content);

    preg_match_all('/\S+/',$new_content,$matches,PREG_SET_ORDER);

    // For each word found in new_content, add it to the words array
    // If already added, increment the number of times it appears
    foreach ($matches as $match) {
        if (!isset($words[$filename][$match[0]]))
            $words[$filename][$match[0]]=0;
        $words[$filename][$match[0]]++;
    }
    // Compare list of words to stop words and remove results
    foreach ($commonWords as $value)
        if (isset($words[$filename][$value]))
            unset($words[$filename][$value]);

    // Total amount of words in each file
    $totalCount = count($words[$filename]);

    // And another item to the list
    $index[] = array(
        'filename' =&gt; $filename,
        'word' =&gt; $words[$filename],
        'all_words_count' =&gt; $totalCount
    );

    // Increment the number of documents by 1
    $num_docs++;

}
// Check if search has been performed, then continue
//if(isset($_GET['search']))
if(!is_null($searchWord))
{
    // Initialise number of results
    $num_results = 0;
    // Check index array for search term
    foreach ($index as $result) {
        if (array_key_exists($searchWord,$result['word']))
        {
            // Add results to results array
            $results[] = array(
                'filename' =&gt; $result['filename'],
                'word' =&gt; $searchWord,
                'count' =&gt; $result['word'][$searchWord],
                'all_words_count' =&gt; $result['all_words_count'],
                'TF' =&gt; (float) $result['word'][$searchWord]/$result['all_words_count']
            );
            // Increment the number of results
            $num_results++;
        }
    }

    //echo $num_results."&lt;br&gt;".$num_docs."&lt;br&gt;";
    // Calculate the total number of documents over the number of documents containing search term
    $DF = $num_docs / $num_results;
    //echo $DF."&lt;br&gt;";
    // Calculate the IDF by using log on the previous (DF) result
    $IDF = (float) log10($DF);
    //echo $IDF."&lt;br&gt;";

    // Create new results array containing the TF-IDF score for each result
    foreach($results as $result) {
        $results2[] = array(
            'filename' =&gt; $result['filename'],
            'word' =&gt; $searchWord,
            'count' =&gt; $result['count'],
            'all_words_count' =&gt; $result['all_words_count'],
            'TF' =&gt; (float) $result['word'][$searchWord]/$result['all_words_count'],
            'TFIDF' =&gt; (float) $result['TF']*$IDF
        );
    }

    // Echos the number of documents the search term was found in
    echo "&lt;br&gt;Search term found in ".$num_results." documents.&lt;br&gt;&lt;br&gt;";

    // Used to get and order the new results array based on the TF-IDF score
    foreach ($results2 as $key =&gt; $row) {
        $count[$key]  = $row['TFIDF'];
    }
    // Sort desc based on TF-IDF score
    array_multisort($count, SORT_DESC, $results2);

    // Echo out the new results array
    foreach($results2 as $result) {
        echo "Found ".$searchWord." in &lt;a href='".$result['filename']."'&gt;".$result['filename']."&lt;/a&gt; ".$result['count']." times. Total words in file: ".$result['all_words_count'].". TF-IDF = ".$result['TFIDF']."&lt;br&gt;";   
    }

    // Calculates the end time of the page loading and deductes the time taken to load page
    $endtime = microtime();
    $endarray = explode(" ", $endtime);
    $endtime = $endarray[1] + $endarray[0];
    $totaltime = $endtime - $starttime; 
    $totaltime = round($totaltime,5);
    echo "&lt;div id='timetaken'&gt;&lt;p&gt;This page loaded in $totaltime seconds.&lt;/p&gt;&lt;/div&gt;";
}
?&gt;
&lt;/div&gt;&lt;!-- close wrapper --&gt;
&lt;/body&gt;
&lt;/html&gt;
</code></pre>
