使用Array的PHP数据库增量搜索

I am learning coding, but I am wanting to make an interactive roster for a local youth camp where upon registering a new camper's personal information (name | birth_year | hobbies | favorite_subject | spirit_animal_explanation | contact_information) the camper can select a "group-me" button that will display the profiles of other campers who have similar hobbies and spirit animals as them as well as being within the same age group (11-13yrs, 14-16yrs,etc).

To achieve this, when a camper enters in his/her information, it is first saved into the database. I then query a SELECT syntax that takes the info from the 'hobbies' and 'spirit_animal_explanation' columns of the camper and filter it of unwanted words and put them in an array (so for 'hobbies' if the camper says "I like Running and Jumping" the the filter will array will only (print_r Array([0]=>running[1]=>jumping).

I then array_merge the two filtered arrays into one so I easily manage the search (some campers like mixing up the hobbies into the spirit_animal_explanation so by having a single reference array for both columns the search can be easier) leaving me with a $compiled variable containing the two merged arrays.

Now the part(s) I am Struggling with: I am able to prepare the reference array for the search, but I don't know the query syntax to begin the search or scanning the hobbies and spirit_animal_explanation columns of every user using the reference array. From previous suggestions I've read up on JOIN queries but I don't see a point in making two tables because the elements of the reference array are not fixed/constant for every camper that executes the search. Also, I have no idea how/when I should add in the age conditional. Finally, the improve latency I want the system to look for the first 10 matching users on the first search session, then the next ten users on the second, etc. How can I go about doing this?

Edit: Using REGEXP, the implode worked, but I am not getting results for some reason. Here is my code:

<?php
    error_reporting(E_ALL ^ E_NOTICE);
    ini_set('display_errors',1);
    ini_set('display_startup_errors',1);

    if(isset($_GET['group-me'])) {

    // run query, get $result
    $query = "SELECT `hobbies`, `spirit_animal_explanation` FROM `users`";
    $result = $con->query($query);

    if ($result) {
      while ($row = $result->fetch_assoc()) {
    // this contains an associative array with the keys, 'hobbies'and 'spirit_animal_explanation'
           $compile = $row;
        }

    $tags1 = explode(" ",strtolower(preg_replace("/[^a-zA-Z]+/", " ", $compile['hobbies'])));
    $tags2 = explode(" ",strtolower(preg_replace("/[^a-zA-Z]+/", " ", $compile['spirit_animal_explanation'])));

//example of word filtering
    $black_list_1 = array_diff($tags1, array('i','a','am','do','really','the','then');
    $black_list_2 = array_diff($tags2, array('i','them','is','at','an','what','because');

//further cleaning
    $clean_1 = array_filter($black_list_1);
    $clean_2 = array_filter($black_list_2);

//combining both arrays into a single array
    $compressed = array_merge($clean_1, $clean_2);

//alter array to be used against string values
    $keywords = implode("|", $compressed);

        $result->close();
    }
    $searchCD = "SELECT * FROM users WHERE field REGEXP '".$keywords."'";
    $searchST = $con->query($searchCD);

    if($searchST) {
      while ($record = $searchST->fetch_assoc()) {

       echo $record['first_name'] . "<br/>" . $record['hobbies'] . "<br/>" . $record['spirit_animal_explanation'] . "<br/>" . $record['favorite_subject'] . "<br/>" . $record['contact_information'];

      }
}

}

   ?>

it's hard to know without seeing the table structure, but what your looking for may be the 'like' clause in mysql.

http://www.tutorialspoint.com/mysql/mysql-like-clause.htm

for example, if the hobby is 'jumping' you may be looking for:

SELECT * FROM sometable where hobby like "%jumping%";

this would match records where the hobby column contains the word "jumping" anywhere in it. ie "jumping and running" and "drinking and jumpting" would both match.

A REGEXP might be more efficient for you, e.g.

$array = array('keyword1', 'keyword2', 'keyword3', 'keyword4');
$keywords = implode("|", $array);

"SELECT * FROM tbl_name WHERE field REGEXP '".$keywords."'";

This will output something like:

"SELECT * FROM tbl_name WHERE field REGEXP 'keyword1|keyword2|keyword3|keyword4'";

Hope this might help you.