How to search a multidimensional array using GET
<p>Hey guys, I've had a lot of help from everyone here and i am really appreciative! I'm trying to create a text file search engine and i think i am on the final stretch now! All i need to do now is to be able to search the multi-dimensional array i've created for a certain word submitted by a form and grabbed with GET, and return the results in highest to lowest order (TF-IDF will come later). I can perform a simple search on the content variable which is not really what i want (see in code for $new_content) but not on the $index array.</p>

<p>Here is my code:</p>

<pre><code>&lt;?php
$starttime = microtime();
$startarray = explode(" ", $starttime);
$starttime = $startarray[1] + $startarray[0];

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;/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
include "commonwords.php";
$index = array();

$words = array();

// All files with a .txt extension
// Alternate way would be "/path/to/dir/*"
foreach (glob("./files/*.txt") as $filename) {

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

    $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);

    foreach ($matches as $match) {
        if (!isset($words[$filename][$match[0]]))
            $words[$filename][$match[0]]=0;
        $words[$filename][$match[0]]++;
    }
    foreach ($commonWords as $value)
        if (isset($words[$filename][$value]))
            unset($words[$filename][$value]);

    $results = 0;

    $totalCount = count($words[$filename]);


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

}

echo '&lt;pre&gt;';
print_r($index);
echo '&lt;/pre&gt;';
if(isset($_GET['search']))
{
    $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>
