删除数组中连续出现的重复项 [英] Removing successive duplicate occurrences in an array
本文介绍了删除数组中连续出现的重复项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有什么办法可以从下面的数组中删除连续的重复项,而只保留第一个?
Is there any way that I can remove the successive duplicates from the array below while only keeping the first one?
该数组如下所示:
$a=array("1"=>"go","2"=>"stop","3"=>"stop","4"=>"stop","5"=>"stop","6"=>"go","7"=>"go","8"=>"stop");
我想要的是一个包含以下内容的数组:
What I want is to have an array that contains:
$a=array("1"=>"go","2"=>"stop","3"=>"go","7"=>"stop");
推荐答案
您可以执行以下操作:
if(current($a) !== $new_val)
$a[] = $new_val;
假设您没有在两个数组之间进行操作,则可以使用 current()
,这比每次对它进行计数以检查 count($ a)-1
Assuming you're not manipulating that array in between you can use current()
it's more efficient than counting it each time to check the value at count($a)-1
这篇关于删除数组中连续出现的重复项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文