1

im trying to remove from my string the comma separeted and multiples strings and then to count strings as total, i have no luck so.. any help will be apreciated, thanks!

Example names [alex,daniel,obama,alex,alex,alex,diana] = 7 names and without duplicates = 4 as total. This i'm trying to achive.

Here's the Php code:

 $q = mysqli_query($db,"SELECT DISTINCT(names) FROM users WHERE uid = 1 ");
     while ($data = mysqli_fetch_array($q)) {
       $names = count($data['names']);
     echo implode(',', array_keys(array_flip(explode(',',$names))));
 }
Fido
  • 41
  • 7
  • 3
    DISTINCT is not a function, it's a keyword. In the SELECT list, it applies to *all* expressions in the SELECT list. That is, there's no need for the extraneous parens around `names`. Those parens have no meaning. And they make no difference (other than to make it look like the author of the SQL believes that DISTINCT is a function.) – spencer7593 Oct 04 '19 at 22:06

1 Answers1

3

If $data['names'] is a comma separated list of names, you can get only the unique values like this:

$names = array_unique(explode(',', $data['names']));
$count = count($names);
$names = implode(',', $names);
echo "$count names: $names\n";

For your sample data, this gives

4 names: alex,daniel,obama,diana

Demo on 3v4l.org

Nick
  • 138,499
  • 22
  • 57
  • 95