I have 2 db queries each build a string of numeric IDs. Then I combine the strings into one and sometime i can get duplicate vaalues in that combined string

$str = $str1.','.$str2;
$str = trim($str, ","); // if I have either one empty
$str = str_replace(",,", ",", $str );

echo $str;

3,25,1153,3

How do I remove duplicates and keep only distinct values in the string?

    You should put them in array and then use [man]array_unique[/man] to get rid of duplicates.

      OK, done.

      $str= explode(",", $str);
      $str= array_unique($str);
      $str= implode(",", $str);
      

        Don't forget to mark this thread resolved (if it is).

        Out of curiosity, could you show us the two db queries? It's possible that you could instead use one query that would give you the string of unique id's.

          I'll mark the thread.

          The db query already check for DISTINCT values, but they query two different tables and the same ID could present in both and multiple times. So, on the query output I only get unique IDs but when i merge the two strings I have a possibility of doubling IDs.

            Write a Reply...