Search code examples
phparrayscsvduplicatesdelimited

Remove duplicate items in array that have comma separating values


I have an array of languages that looks like this: (this data is pulled from mysql stored as: e.g. field value: greek, polish)

 Array
(
[0] => 
[1] => french
[2] => french, greek
[3] => german
[4] => greek
[5] => greek, polish
[6] => italian
[7] => spanish
[8] => spanish, italian
[9] => spanish, portuguese
)

I have tried using array_unique($languages) but the output stays the same.

I assume it is because of the comma.

Any idea on how to just have unique values? e.g. one each of: french, greek, german, polish, italian, spanish, and portuguese?


Solution

  • Make a plain array from input

    $res = array();
    foreach ($array  as $item)
      $res = array_merge($res, explode(', ', $item)); 
    

    and then use array_unique($res)