第一種方法:
<textarea id="list" class="toolarea">365建站網(wǎng) theartemis.cn www.baidu.com theartemis.cn 365建站網(wǎng) soft.365jz.com theartemis.cn tool.365jz.com www.baidu.com soft.365jz.com tool.365jz.com 365建站網(wǎng) theartemis.cn</textarea> <input type="button" onclick="process()" value="去重處理" class="bt-green"> <script type="text/javascript"> function unique(ary) { var i = 0, gid='_'+(+new Date)+Math.random(), objs = [], hash = { 'string': {}, 'boolean': {}, 'number': {} }, p, l = ary.length, ret = []; for (; i < l; i++) { p = ary[i]; if (p == null) continue; tp = typeof p; if (tp in hash) { if (!(p in hash[tp])) { hash[tp][p] = 1; ret.push(p); } } else { if (p[gid]) continue; p[gid]=1; objs.push(p); ret.push(p); } } for(i=0,l=objs.length;i<l;i++) { p=objs[i]; p[gid]=undefined; delete p[gid]; } return ret; } function process(){ list = document.getElementById('list'); arr = list.value.split('\n'); arr = unique(arr); list.value = ""; for(key in arr){ list.value += arr[key] + '\n'; } alert("處理完成!"); } </script>
第二種方法
function sx(){ var rntArray=[],temp,hasValue; var array=document.getElementById("neirong").value.split("\n"); for(var i in array){ temp=array[i]; hasValue=false; for(var j in rntArray){ if(temp===rntArray[j]){ hasValue=true; break; } } if(hasValue===false){ rntArray.push(temp); } } document.getElementById("neirong1").value=rntArray.join("\n"); }
php數(shù)組函數(shù)序列之a(chǎn)rray_unique() - 去除數(shù)組中重復(fù)的元素值
array_unique() 定義和用法
array_unique() 函數(shù)移除數(shù)組中的重復(fù)的值,并返回結(jié)果數(shù)組。
當(dāng)幾個(gè)數(shù)組元素的值相等時(shí),只保留第一個(gè)元素,其他的元素被刪除。
返回的數(shù)組中鍵名不變。
語法
array_unique(array)
參數(shù) 描述
array 必需。規(guī)定輸入的數(shù)組。
說明
array_unique() 先將值作為字符串排序,然后對每個(gè)值只保留第一個(gè)遇到的鍵名,接著忽略所有后面的鍵名。這并不意味著在未排序的 array 中同一個(gè)值的第一個(gè)出現(xiàn)的鍵名會(huì)被保留。
提示和注釋
注釋:被返回的數(shù)組將保持第一個(gè)數(shù)組元素的鍵類型。
例子
<?php
$a=array("a"=>"Cat","b"=>"Dog","c"=>"Cat");
print_r(array_unique($a));
?>
輸出:
Array ( [a] => Cat [b] => Dog )
例一:
<?php
$input = array("a" => "green","", "red","b" => "green", "","blue", "red","c" => "witer","hello","witer");
//$result = array_unique($input); //去除重復(fù)元素
$result = a_array_unique($input); //只留下單一元素
foreach($result as $aa)
{
echo $aa."<br />";
}
function a_array_unique($array)//寫的比較好(寫方法)
{
$out = array(); //定義變量out為一個(gè)數(shù)組
foreach ($array as $key=>$value) //將$array數(shù)組按照$key=>$value的樣式進(jìn)行遍歷
{
if (!in_array($value, $out))//如果$value不存在于out數(shù)組中,則執(zhí)行
{
$out[$key] = $value; //將該value值存入out數(shù)組中
}
}
return $out; //最后返回?cái)?shù)組out
}
?>
PHP數(shù)組去除重復(fù)項(xiàng) 有個(gè)內(nèi)置函數(shù)array_unique (),但是php的 array_unique函數(shù)只適用于一維數(shù)組,對多維數(shù)組并不適用,以下提供一個(gè)二維數(shù)組 的 array_unique函數(shù)
例二:
function unique_arr($array2D,$stkeep=false,$ndformat=true)
{
// 判斷是否保留一級數(shù)組鍵 (一級數(shù)組鍵可以為非數(shù)字)
if($stkeep) $stArr = array_keys($array2D);
// 判斷是否保留二級數(shù)組鍵 (所有二級數(shù)組鍵必須相同)
if($ndformat) $ndArr = array_keys(end($array2D));
//降維,也可以用implode,將一維數(shù)組轉(zhuǎn)換為用逗號連接的字符串
foreach ($array2D as $v)
{
$v = join(",",$v);
$temp[] = $v;
}
//去掉重復(fù)的字符串,也就是重復(fù)的一維數(shù)組
$temp = array_unique($temp);
//再將拆開的數(shù)組重新組裝
foreach ($temp as $k => $v)
{
if($stkeep) $k = $stArr[$k];
if($ndformat)
{
$tempArr = explode(",",$v);
foreach($tempArr as $ndkey => $ndval) $output[$k][$ndArr[$ndkey]] = $ndval;
}
else $output[$k] = explode(",",$v);
}
return $output;
}
如對本文有疑問,請?zhí)峤坏浇涣髡搲?,廣大熱心網(wǎng)友會(huì)為你解答?。?點(diǎn)擊進(jìn)入論壇