如何从字符串中删除分解的逗号分隔数组中的重复值?

时间:2016-03-30 20:30:55

标签: php arrays

$NoteKey = ",";

(loop) if ... then{
    $NoteKey .= "</td><td>";
    $NoteKey .= $val2;
    $NoteKey .= "</td><td>";
    $NoteKey .= $val3;
    $NoteKey .= "</td><td>";
    $NoteKey .= "</td></tr>";
    $NoteKey .= ",";
}

$NoteKey_trim = trim($NoteKey, ",");
$NoteKey_clean = explode(",",$NoteKey_trim);
$NoteKey_unique = array_unique($NoteKey_clean);

问题在于没有输出......直到$NoteKey_clean然后没有输出......

2 个答案:

答案 0 :(得分:0)

$NoteKey_trim = trim($NoteKey, ",");// delete ","
$NoteKey_clean = explode(",",$NoteKey_trim); // $NoteKey_trim don't have ","  
//explode can't find ","
$NoteKey_unique = array_unique($NoteKey_clean);
$NoteKey_trim = trim($NoteKey);

if(strpos(",",$NoteKey_trim)!==false){  // $NoteKey_trim has "," 
  $NoteKey_clean = explode(",",$NoteKey_trim);
  $NoteKey_unique = array_unique($NoteKey_clean);
}

答案 1 :(得分:0)

String[] functions= commaSeperatedString.split(",");

        List<String> uniqueFunctions = new ArrayList<>();

        for (String function : functions) {
            if ( !uniqueFunctions.contains(function.trim())) {
                uniqueFunctions.add(function.trim());
            }
        }
        return String.join(",",uniqueFunctions);