ASP 过滤数组重复数据函数(加强版)_javascript技巧
函数代码:
'过滤数组重复函数名称:array_no(cxstr1,cxstr2,cxstr3)
'cxstr1:任意的字符串,自动识别
'cxstr2:cxstr1中分割符号。
'cxstr3:提取结果中的某一位置字串,等于0时返回为全部,大于数组下标时返回最后.
'使用于二维数组
'*******************************************************
function array_no(cxstr1,cxstr2,cxstr3)
if len(cxstr3) > 0 then
if not IsNumeric(cxstr3) then
array_no = "对不起,参数3类型必需为数字"
Exit Function
end if
else
array_no = "对不起,参数3类型必需为数字"
Exit Function
end if
if isarray(cxstr1) then
array_no = "对不起,参数1不能为数组"
Exit Function
end if
if cxstr1 = "" or isempty(cxstr1) then
array_no = "没有数据"
Exit Function
end if
ss = split(cxstr1,cxstr2)
cxs=cxstr2&ss(0)&cxstr2
sss=cxs
for m = 0 to ubound(ss)
cc = cxstr2&ss(m)&cxstr2
if instr(sss,cc)=0 then
sss = sss&ss(m)&cxstr2
end if
next
array_no = right(sss,len(sss)-len(cxstr2))
array_no = left(array_no,len(array_no)-len(cxstr2))
if cxstr3 0 then
cx_sp = split(array_no,cxstr2)
if cxstr3 > ubound(cx_sp) then
array_no = cx_sp(ubound(cx_sp))
else
array_no = cx_sp(cxstr3)
end if
end if
end function%>
下面是测试代码:
s2 = "1,2,3,11,22,33,12,13,14,11,33,333,14"
s3 = ""
s4 = "sdf,abc,12,2,2,abc"
s5 = split(s4)
response.write "字串为字符时:"&array_no(s1,",",0)&"
"
response.write "字串为数字时:"&array_no(s2,",",0)&"
"
response.write "字串为空时:"&array_no(s3,",",0)&"
"
response.write "字串为混合时:"&array_no(s4,",",0)&"
"
response.write "字串为数组时:"&array_no(s5,",",0)&"
"
response.write "字串为未知变量时:"&array_no(s33,",",0)&"
"
response.write "提取某一位时,没有超过下标时:"&array_no(s1,",",2)&"
"
response.write "提取某一位时,超过下标时:"&array_no(s1,",",200)&"
"%>
测试结果:
字串为字符时:abc,aa,bb,cdef,bc,abcdef,hhgg,gggg,cde,edc
字串为数字时:1,2,3,11,22,33,12,13,14,333
字串为空时:没有数据
字串为混合时:sdf,abc,12,2
字串为数组时:对不起,参数1不能为数组
字串为未知变量时:没有数据
提取某一位时,没有超过下标时:bb
提取某一位时,超过下标时:edc
脚本之家增强版本: 解决了数组常见错误
'*******************************************************
'过滤数组重复函数名称:array_no(cxstr1,cxstr2,cxstr3)
'cxstr1:任意的字符串,自动识别
'cxstr2:cxstr1中分割符号。
'cxstr3:提取结果中的某一位置字串,等于0时返回为全部,大于数组下标时返回最后.
'使用于二维数组
'*******************************************************
function array_no(cxstr1,cxstr2,cxstr3)
if len(cxstr3) > 0 then
if not IsNumeric(cxstr3) then
array_no = "对不起,参数3类型必需为数字"
Exit Function
end if
else
array_no = "对不起,参数3类型必需为数字"
Exit Function
end if
if isarray(cxstr1) then
array_no = "对不起,参数1不能为数组"
Exit Function
end if
if cxstr1 = "" or isempty(cxstr1) then
array_no = "没有数据"
Exit Function
end if
do while instr(cxstr1,",,")>0
cxstr1=replace(cxstr1,",,",",")
loop
if right(cxstr1,1)="," then
cxstr1=left(cxstr1,len(cxstr1)-1)
end if
ss = split(cxstr1,cxstr2)
cxs=cxstr2&ss(0)&cxstr2
sss=cxs
for m = 0 to ubound(ss)
cc = cxstr2&ss(m)&cxstr2
if instr(sss,cc)=0 then
sss = sss&ss(m)&cxstr2
end if
next
array_no = right(sss,len(sss)-len(cxstr2))
array_no = left(array_no,len(array_no)-len(cxstr2))
if cxstr3 0 then
cx_sp = split(array_no,cxstr2)
if cxstr3 > ubound(cx_sp) then
array_no = cx_sp(ubound(cx_sp))
else
array_no = cx_sp(cxstr3)
end if
end if
end function
s1 = "abc,aa,bb,cdef,bc,abcdef,hhgg,gggg,cde,edc,333,,,,,333,7,,,,"
s2 = "1,2,3,11,22,33,12,13,14,11,33,333,14,333,,,,,333,7,,,,"
s3 = ""
s4 = "sdf,abc,12,2,2,abc,333,,,,,333,7,,,,"
s5 = split(s4)
response.write "字串为字符时:"&array_no(s1,",",0)&"
"
response.write "字串为数字时:"&array_no(s2,",",0)&"
"
response.write "字串为空时:"&array_no(s3,",",0)&"
"
response.write "字串为混合时:"&array_no(s4,",",0)&"
"
response.write "字串为数组时:"&array_no(s5,",",0)&"
"
response.write "字串为未知变量时:"&array_no(s33,",",0)&"
"
response.write "提取某一位时,没有超过下标时:"&array_no(s1,",",2)&"
"
response.write "提取某一位时,超过下标时:"&array_no(s1,",",200)&"
"
%>
主要是增加了判断
do while instr(cxstr1,",,")>0
cxstr1=replace(cxstr1,",,",",")
loop
if right(cxstr1,1)="," then
cxstr1=left(cxstr1,len(cxstr1)-1)
end if

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

热门话题

使用foreach循环去除PHP数组中重复元素的方法如下:遍历数组,若元素已存在且当前位置不是第一个出现的位置,则删除它。举例而言,若数据库查询结果存在重复记录,可使用此方法去除,得到不含重复记录的结果。

PHP中深度复制数组的方法包括:使用json_decode和json_encode进行JSON编码和解码。使用array_map和clone进行深度复制键和值的副本。使用serialize和unserialize进行序列化和反序列化。

PHP数组键值翻转方法性能对比表明:array_flip()函数在大型数组(超过100万个元素)下比for循环性能更优,耗时更短。手动翻转键值的for循环方法耗时相对较长。

PHP的array_group_by函数可根据键或闭包函数对数组中的元素分组,返回一个关联数组,其中键是组名,值是属于该组的元素数组。

在PHP中执行数组深度复制的最佳实践是:使用json_decode(json_encode($arr))将数组转换为JSON字符串,然后再将其转换回数组。使用unserialize(serialize($arr))将数组序列化为字符串,然后将其反序列化为新数组。使用RecursiveIteratorIterator迭代器对多维数组进行递归遍历。

多维数组排序可分为单列排序和嵌套排序。单列排序可使用array_multisort()函数按列排序;嵌套排序需要递归函数遍历数组并排序。实战案例包括按产品名称排序和按销售量和价格复合排序。

PHP的array_group()函数可用于按指定键对数组进行分组,以查找重复元素。该函数通过以下步骤工作:使用key_callback指定分组键。可选地使用value_callback确定分组值。对分组元素进行计数并识别重复项。因此,array_group()函数对于查找和处理重复元素非常有用。

PHP数组合并去重算法提供了并行的解决方案,将原始数组分成小块并行处理,主进程合并块的结果去重。算法步骤:分割原始数组为均等分配的小块。并行处理每个块去重。合并块结果并再次去重。
