Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/257.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP数组,仅删除相邻的重复项_Php_Arrays - Fatal编程技术网

PHP数组,仅删除相邻的重复项

PHP数组,仅删除相邻的重复项,php,arrays,Php,Arrays,所以基本上,我正在寻找一种快速的方法,只删除相邻的重复项 $arr = array(1, 2, '...', '...', '...', 6, 7, 8, '...', 10); array_unique creates: array(1, 2, '...', 6, 7, 8, 10); I want the following: array(1, 2, '...', 6, 7, 8, '...', 10); 更改$x!==$以前的条件,以适合您对“重复”的首选定义。例如,array\u un

所以基本上,我正在寻找一种快速的方法,只删除相邻的重复项

$arr = array(1, 2, '...', '...', '...', 6, 7, 8, '...', 10);
array_unique creates: array(1, 2, '...', 6, 7, 8, 10);
I want the following: array(1, 2, '...', 6, 7, 8, '...', 10);

更改
$x!==$以前的
条件,以适合您对“重复”的首选定义。例如,
array\u unique
执行松散类型的比较。

我会这样做:

$result = array();
$first = true;
foreach ($array as $x) {
    if ($first || $x !== $previous) $result[] = $x;
    $previous = $x;
    $first = false;
}

好的,没有现成的函数,所以写一个小的循环脚本。当遇到非重复项时,需要将
$first
设置回
true
。@Barmar不,这不是它的用途。
<?php

$arr = array(1, 2, '...', '...', '...', 6, 7, 8, '...', 10);

$el = $arr[0];

$out = $arr;

for ($i=1, $c = count($out); $i<$c; ++$i) {
    if ($arr[$i] == $el) {
        unset($out[$i]);
    }
    else {
        $el = $out[$i];
    }
}

$out = array_values($out);

foreach ($out as $i) {
    echo $i."<br />";
}
1
2
...
6
7
8
...
10