如何删除数组中的重复元素(asp教程,js,php教程)
<html xmlns="http://www.111cn.net/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
</head>
<body>
js删除数据组中重复的元素
<script language="javascript教程">
var fruit = new Array(3);
fruit[0] = new Array("苹果", "a");
fruit[1] = new Array("苹果", "a");
fruit[2] = new Array("橘子", "2");
for (i=0; i < fruit.length; i++) {
for (j=0; j < fruit[0].length; j++) {
document.write("fruit [", i, "][", j, "] = ", fruit[i][j],"");
}
document.write("");
}
</script>
</body>
</html>
<?
//下面是一段php删除数据组中重复的元素
//方法一,用php自带的array_flip超简单
print_r(array_flip(array_flip($array)));
//利用自定函数来实现
function delsame(&$array)
{
$i = 0;
while(isset($array[$i]))
{
$j = $i + 1;
while(isset($array[$j]))
{
if($array[$i] == $array[$j]) //如果发现后面有重复的元素
{
delmember($array, $j); //把它删除
$j--; //重新检查补上来的元素是否是重复的
}
$j ++;
}
$i ++;
}
}
//
$array = array(1,1,1,1,2,2,3);
//print_r(delsame( $array ) );function delmember(&$array, $id)
{
$size = count($array);
for($i = 0; $i <$size - $id - 1; $i ++)
{
$array[$id + $i] = $array[$id + $i + 1];
}
unset($array[$size - 1]);
}
//一款asp的删除数组中重复元素的函数
<%
Function moveR(farray,sarray)a = Split(farray, ",")
Set dic = CreateObject("Scripting.Dictionary")
For k=0 To UBound(a)
If a(k)<> "" Then dic.Add "_" & a(k), a(k)
Next
a = Split(sarray, ",")
For k=0 To UBound(a)
If a(k)<> "" Then
If dic.Exists("_" & a(k)) Then
dic.Remove "_" & a(k)
End If
End If
Nextitems = dic.Items()
Set dic = Nothing
moveR=Join(items, ",")
End Function
n1 = "a,b,1,11,12,13,14,15,16,17,19,20,22"
n2 = "a,1,12,14,18,19,20"
response.write mover(n1,n2)
%>
?>