How to delete repeated elements in an array (asp, js, and php)
<Html xmlns = "http://www.111cn.net/1999/xhtml">
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312"/>
</Head>
<Body>
Js deletes repeated elements in a data Group
<Script language = "javascript tutorial">
Var fruit = new Array (3 );
Fruit [0] = new Array ("Apple", "");
Fruit [1] = new Array ("Apple", "");
Fruit [2] = new Array ("Orange", "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>
<?
// Below is a piece of php to delete repeated elements in the data group
// Method 1: php array_flip is super simple.
Print_r (array_flip ($ array )));
// Use a custom function
Function delsame (& $ array)
{
$ I = 0;
While (isset ($ array [$ I])
{
$ J = $ I + 1;
While (isset ($ array [$ j])
{
If ($ array [$ I] = $ array [$ j]) // if duplicate elements are found
{
Delmember ($ array, $ j); // delete it
$ J --; // re-check whether the supplemented elements are repeated.
}
$ J ++;
}
$ I ++;
}
}
//
$ Array = array (, 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]);
}
// An asp function used to delete repeated elements in an array
<%
Function moveR (farray, sarray)
A = Split (farray ,",")
Set dic = CreateObject ("Scripting. Dictionary ")
For k = 0 To UBound ()
If a (k) <> "Then dic. Add" _ "& a (k), a (k)
Next
A = Split (sarray ,",")
For k = 0 To UBound ()
If a (k) <> "Then
If dic. Exists ("_" & a (k) Then
Dic. Remove "_" & a (k)
End If
End If
Next
Items = dic. Items ()
Set dic = Nothing
MoveR = Join (items ,",")
End Function
N1 = "a, B, 22"
N2 = ""
Response. write mover (n1, n2)
%>
?>