$result = mysql_query("SELECT word, definition FROM wordtable ORDER by word ASC"); $wordlist = array(); while($row = mysql_fetch_row($result) $wordlist[$row[0]] = $row[1]; foreach ($wordlist as $word => $defintion) { echo "$word"; $definition_words = explode($definition, ' '); foreach($definition_words as $this_word) { if ( isset($wordlist[$this_word]) ) echo "$this_word "; else echo "$this_word "; } echo '
'; }