I am trying to write a function that takes an array and removes the duplicates, returning the result as a comma separated string. After some Googling, I found its possible to do itwith a single line ( return implode(",",unique_array($arr)); ). However, that doesn't help me figure out was wrong with my original code. So perhaps you could tell me the logical errors in my original code that causes it to return all the values, including the duplicates?
Thank you! :-)
<?php
function GetUniqueValues($arr) {
$x=0;
foreach($arr as $i) {
if(!in_array($i, $arr2)) {
$arr2[x]=$i;
$x++;
}
}
$str = implode(",",$arr);
return $str;
}
$arr = array(1,2,2,3,2,4,4,5,4,7,6,8,9);
echo GetUniqueValues($arr);
?>