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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 请问如何在openpcdet上对KITTI数据集的测试集进行结果评估?
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗