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;