PHP - Using implode, explode, and array_unique to autopopulate a dropdown menu from two SQL columns of comma-separated keywords
<p>I have an SQL database with a "category" keyword (only one allowed) and "issues" keywords (multiple comma-separated words).  I am trying to make a auto-populating drop-down keyword select menu by selecting all the  keywords from the "category" and "issues" columns, turning both returned arrays into comma-separated strings with implode, then combining the strings and exploding the comma-separated strings into an array, while removing duplicate entries with array_unique.</p>

<p>But it's not working. I've tried several approaches.  Here is my latest.  It is returning SOME values from the column but not all, and I can't figure out why.  Perhaps array_unique isn't working the way I want it to work, or I am messing up the conversion to strings and back into an array?  Is there a simpler way to do this?  I have searched all over and can't find a good example anywhere.</p>

<p>Here is the code I have working now...</p>

<pre><code>&lt;?
$dropdownsql = "SELECT DISTINCT category FROM database";
$keywords = mysql_query($dropdownsql);
while($row = mysql_fetch_array($keywords))
{ 
  echo "&lt;option value=\"".$row['category']."\"&gt;".$row['category']."&lt;/option&gt;\n  ";
}
?&gt;
</code></pre>

<p>While this works for the one-word category keywords, it obviously can't handle multiple SQL columns or comma-separated keywords within those columns.  Here's my attempt to do that in the most straightforward way:</p>

<pre><code>&lt;?
$dropdownsql = "SELECT DISTINCT category FROM database";
$dropdownsql2 = "SELECT DISTINCT issues FROM database";

//run sql queries separately.  Ideally they would be combined into one right?
$rs = mysql_query($dropdownsql);
$rs2 = mysql_query($dropdownsql2);
$row = mysql_fetch_array($rs);
$raw = mysql_fetch_array($rs2);

//then implode the resulting arrays, placing commas &amp; spaces so they'll match
$rows = implode(", ", $row);
$raws = implode(", ", $raw);

//try to concatenate the strings of comma-separated keywords
$keywordvaluesstring = $rows.$raws;

//then explode the concatenated string back into array
$keywordvalue = explode(", ",$keywordvaluesstring);

//then keep only one copy of duplicated keywords
$values = array_unique($keywordvalue, SORT_REGULAR);

//and finally echo the keywords into a dropdown 
foreach($values as $value){ 
  echo "&lt;option value=\"".$value."\"&gt;".$value."&lt;/option&gt;\n  ";
}
?&gt;
</code></pre>

<p>WHAT AM I DOING WRONG!!!!????</p>