How to delete repeated elements in an array (asp, js, php)

Source: Internet
Author: User

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)
%>

?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.