我有这样的数组
{
0: "abc"
1: "abc"
5: "abc"
6: "def"
7: "ghk"
8: "abc"
}
我想删除adjecent类似的值。它喜欢
{
0: "abc"
6: "def"
7: "ghk"
8: "abc"
}
我尝试使用
$p = array_unique($array);
但它变成了
{
0: "abc"
6: "def"
7: "ghk"
}
我不想删除8:“abc”,因为它不是相似的值。
我可以使用任何功能吗?谢谢:))
答案 0 :(得分:5)
$myArray = array(
0 => "abc",
1 => "abc",
5 => "abc",
6 => "def",
7 => "ghk",
8 => "abc",
);
$previous = NULL;
$newArray = array_filter(
$myArray,
function ($value) use (&$previous) {
$p = $previous;
$previous = $value;
return $value != $p;
}
);
var_dump($newArray);
答案 1 :(得分:2)
你可以这样做:
$myArray = array(
0 => "abc",
1 => "abc",
5 => "abc",
6 => "def",
7 => "ghk",
8 => "abc",
);
$newArray = array();
$previous = '';
foreach ($myArray as $myArray_value) {
if ($previous !== $myArray_value)
$newArray[] = $myArray_value;
$previous = $myArray_value;
}
print_r($newArray);
希望这可以帮助你:)