I have following code to remove adjacent duplicates from an array
$myArray = array(
0 => 0,
1 => 0,
2 => 1,
5 => 1,
6 => 2,
7 => 0,
8 => 0,
);
$previtem= NULL;
$newArray = array_filter(
$myArray,
function ($currentItem) use (&$previtem) {
$p = $previtem;
$previtem= $currentItem;
return $currentItem!= $p ;
}
);
echo "<pre>";
print_r($newArray);
Problem
Required Output.
Array
(
[0] => 0
[2] => 1
[6] => 2
[7] => 0
)
Actual output
Array
(
[2] => 1
[6] => 2
[7] => 0
)
How to get required output without modifying my code much?? or is there any other better way?
Thanks