I working on a function which filters a multi dimensional array, if the successive arrays have same value, I need to remove the arrays in between and get only the first and last of the successive arrays with duplicate values.
I cant figure out how to do this.
Hope you can help me.
Thanks.
SAMPLE CODE
function removeDuplicates($array){
$result = [];
$count = 0;
$firstValue = null;
for ($i = 0; $i < count($array); $i++) {
if($array[$i]['id'] == firstValue){
$result[] = $array[$i]);
}else{
// do action
}
}
return $array;
}
Sample Array
Array (
[0] => Array (
[key] => 0
[value] => 25
)
[1] => Array (
[key] => 1
[value] => 25
)
[2] => Array (
[key] => 2
[value] => 25
)
[3] => Array (
[key] => 3
[value] => 33
)
[4] => Array (
[key] => 4
[value] => 45
)
[5] => Array (
[key] => 5
[value] => 66
)
[6] => Array (
[key] => 6
[value] => 66
)
[7] => Array (
[key] => 7
[value] => 66
)
[8] => Array (
[key] => 8
[value] => 66
)
[9] => Array (
[key] => 9
[value] => 55
)
)
Here the first 3 arrays have same values (25), I need to remove the 2nd array and retain the 1st and 3rd, also with the 6th to 9th only retain the 6th and 9th array.
Expected Result
Array (
[0] => Array (
[key] => 0
[value] => 25
)
[2] => Array (
[key] => 2
[value] => 25
)
[3] => Array (
[key] => 3
[value] => 33
)
[4] => Array (
[key] => 4
[value] => 45
)
[5] => Array (
[key] => 5
[value] => 66
)
[8] => Array (
[key] => 8
[value] => 66
)
[9] => Array (
[key] => 9
[value] => 55
)
)