我使用数组运行php mssql查询代码,但输出不删除相同的字符。我的代码在这里
$querysfzzrf88 = " SELECT * FROM Style WHERE StyleNo='$StyleNo'";
$resultsfzzrf88 = mssql_query($querysfzzrf88);
//display the results
while($rowsfzzrf88 = mssql_fetch_array($resultsfzzrf88))
{
$Status88 = $rowsfzzrf88["Status"];
echo $Status88;
}
输出
LNNNMMMMMMM
另外
var_dump($Status88);
给这样的
string(1) "N" string(1) "N" string(1) "N" string(1) "L"
我想要输出
LNM
答案 0 :(得分:1)
$str = "LNNNMMMMMMM";
echo count_chars($str, 3);
第二个参数是mode
,
3 - a string containing all unique characters is returned.
请参阅演示here
修改强>
试试这个,
$str = " LNNNMMMMMMM";
echo preg_replace("/(.)\\1+/", "$1", $str); or echo preg_replace('/(.)\1+/', '$1', $str);
请参阅演示here
答案 1 :(得分:0)
对不起,如果我误解了你的问题,这是你所期待的:
<?php
$chars = 'LNNNMMMMMMM';
$array = str_split($chars);
$array = array_count_values($array);
$array = array_flip($array);
echo implode('', $array);
?>
它产生了
结果LNM
更新:
//display the results
$duplicated88 = $Status88 = $unique88 = array();
while($rowsfzzrf88 = mssql_fetch_array($resultsfzzrf88)){
$duplicated88[] = $Status88[] = $unique88[] = $rowsfzzrf88["Status"];
}
sort($unique88);
sort($duplicated88);
$unique88 = implode('', array_unique($unique88));
print($unique88);
答案 2 :(得分:0)
在mysql查询中使用DISTINCT
以避免重复值:
$querysfzzrf88 = " SELECT DISTINCT status FROM Style WHERE StyleNo='$StyleNo'";
$resultsfzzrf88 = mssql_query($querysfzzrf88);
//display the results
while($rowsfzzrf88 = mssql_fetch_array($resultsfzzrf88))
{
$Status88 = $rowsfzzrf88["Status"];
echo $Status88;
}