Array Help

Discussion in 'PHP' started by aklead, Apr 25, 2009.

  1. #1
    Hello,

    I wanted to know how to do the following using PHP and MySQL. Basically I want to break up a string into single words and put them into an array. Then, I want to cycle through each word in that array and run a MySQL query using that word. Then after I have run a query for every word in that initial array, I want to compare them using array_intersect and finally echo out the records that all the arrays had in common.

    I just cannot get my head wrapped around working with these arrays. Any help would be great!

    Thanks in advanced!
     
    aklead, Apr 25, 2009 IP
  2. nayes84

    nayes84 Member

    Messages:
    34
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    43
    #2
    to break string into array or single words use
    
    $arr=explode(" ",$str);
    
    PHP:
    then to loop through the array use
    
    foreach($arr as $word)
    {
    //put here the mysql query you need to make
    //you can use $word to make up your query as required
    }
    
    PHP:
    hope I could help;)
     
    nayes84, Apr 25, 2009 IP
  3. aklead

    aklead Peon

    Messages:
    30
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Excellent! but how would I store the SQL results into individual arrays, so that I can compare the contents at the end?
     
    aklead, Apr 25, 2009 IP
  4. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #4
    You could probably do something like this:
    
    <?php
    $str = ("test1 test2 test3 test4 test5");
    $arr = explode(" ",$str);
    
    $result = array();
    foreach ($arr as $word) {
    	$result[] = $word;
    	}
    print_r($result);
    	
    ?>
    
    PHP:
    note, this is pseudocode, and you would have to modify it to add the queries and add the result of the queries to the result-array. It should be a starting point, though.
     
    PoPSiCLe, Apr 26, 2009 IP