PHP数组数组中的删除重复的连续occurances [英] php array removing successive duplicate occurances in an array

查看:141
本文介绍了PHP数组数组中的删除重复的连续occurances的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想问一下这里的问题是:

反正是有,我可以从下而只保留第一个数组中删除连续重复?

阵列如下:

<$p$p><$c$c>$a=array(\"1\"=>\"go\",\"2\"=>\"stop\",\"3\"=>\"stop\",\"4\"=>\"stop\",\"5\"=>\"stop\",\"6\"=>\"go\",\"7\"=>\"go\",\"8\"=>\"stop\");

我要的是有包含数组:

  $ a =阵列(1=&gt;中去,2=&gt;中停,3=&gt;中去,7 = GT;停止);

任何建议会有所帮助。问候。


解决方案

您只需做一些这样的:

 如果(电流($ A)!== $ new_val)
    $ A [] = $ new_val;

假设你没有操纵之间,你可以使用数组电流()这比每次计算它在计数($ A)-1

The question i would like to ask here is:

Is there anyway that i can remove the successive duplicates from the array below while only keeping the first one?

The array is shown below:

$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");

Any suggestions would help. Regards.

解决方案

You can just do something like:

if(current($a) !== $new_val)
    $a[] = $new_val;

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

这篇关于PHP数组数组中的删除重复的连续occurances的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆