从mysql中删除重复的逗号分隔值

Fag*_*ele -1 php mysql

如何删除从数据库返回的重复逗号分隔值?

我有这种格式:

XL,L,XXL
XL
M,L,XL
L,XL,M
Run Code Online (Sandbox Code Playgroud)

我试过的:

$new_str = array_unique(array_map('trim', explode(',', $srow['size'])));
$string = implode(',', $new_str);
Run Code Online (Sandbox Code Playgroud)

但我仍然得到重复的值。有更好的方法吗?

Nic*_*ick 5

由于您的数据在数据库中存储为逗号分隔的值,因此您需要在循环从数据库中获取时聚合所有值,然后删除重复项,例如

while ($srow = $result->fetch_assoc()) {
    $sizes[] = $srow['size'];
    // do other stuff with fetched data
    // ...
}
$sizes = array_unique(array_map('trim', explode(',', implode(',', $sizes))));
sort($sizes);
$string = implode(',', $sizes);
Run Code Online (Sandbox Code Playgroud)

3v4l.org 上的演示(使用模拟数据库获取)