douxiegan6468 2013-02-23 19:30
浏览 90
已采纳

在PHP中删除逗号分隔列表中的重复值

How can I remove duplicate values within a comma separated list in PHP?

I have 2 versions of a customer questionnaire - one is more detailed than the other - and in one they are asked the items they like and in the other they are asked to specify the items they like in ranked order. In this particular example, one of the columns ('items_like') may contain several values in a comma separated list. There are two other scenarios where each of the columns may contain several values in a comma separated list.

In order to display a list of all the items a customer likes, I am concatenating the 4 columns when the information is fetched from the db. How can I remove any duplicate values from the combined comma separated list in? I'd prefer doing this in PHP than Javascript

try {  
$stmt = $conn->prepare("SELECT * FROM customer_info WHERE user_id = :user_id"); 
$stmt->bindValue(':user_id', $user_id); 
$stmt->execute();
}catch(PDOException $e) {echo $e->getMessage();}
$row = $stmt->fetch();
$search = array('_', ',', 'Null');
$replace = array(' ', ', ', '');
$rows = str_replace($search, $replace, $row);

$merged_likes = $rows['items_like']. ",  " . $rows['first_like']. ",  " .     $rows['second_like']. ",  " .  $rows['third_like'];
echo $merged_likes; 
  • 写回答

1条回答 默认 最新

  • douqiaosu0280 2013-02-23 21:23
    关注

    When dealing with lists of things, it's easiest to use arrays. Comma-separated strings work alright for displaying things, but they're no fun to work with in code. With that in mind, I would recommend not even touching a comma until you finally display the data out to the screen.

    Unfortunately, it sounds like you already have comma separated strings stored in your database. If this is something you can change, you may want to look into an alternative database structure using multiple tables with a one-to-many relationship between users and things they like. There's a lot that could be said about this, but it's not the subject of your question. The basic concept is called database normalization, as Havelock has already mentioned.

    In the event the database structure cannot be changed and you must work the comma-separated lists, simply undo the comma separation and get them back into arrays in PHP. To do this, you'd use the explode() function:

    $teststring = '1,2,3';
    $test = explode(',', $asdfstring); // array(1, 2, 3)
    

    Once you have arrays, PHP has some handy features for sorting, removing duplicates, etc. The one you're probably most interested in here is array_unique().

    To put it back into a comma-separated list for display, simply use implode(), which does the opposite of explode() by taking an array and joining it with the separator of your choice (e.g., comma).

    Now since you have multiple columns you're dealing with here, and you only want one list, you're going to have to build the array in multiple steps. If every one of these columns can contain multiple values, you can do the explosion thing on all of them:

    $items_like = explode(',', $rows['items_like']);
    $first_like = explode(',', $rows['first_like']);
    $second_like = explode(',', $rows['second_like']);
    $third_like = explode(',', $rows['third_like']);
    $merged = array_merge($items_like, $first_like, $second_like, $third_like);
    

    Or if first, second, and third will only contain one item, you could do this:

    $items_like = explode(',', $rows['items_like']);
    $other_likes = array($rows['first_like'], $rows['second_like'], $rows['third_like']);
    $merged = array_merge($items_like, $other_likes);
    

    Now remove duplicates:

    $likes = array_unique($merged);
    

    Now we're finally ready to add commas:

    echo implode(', ', $likes);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?