I need to search the first unique value and change the succeeding value by space. how can I do it? I just need it in listing report without duplicating display of the values.
$array = array("a", "a", "a", "b", "b", "b", "b");
how to find "a" and change the suceeding a by " "
how to find "b" and change the suceeding b by " "
OUTPUT: $array = array("a", " ", " ", "b", " ", " ", " ");
Thank you.
I'm sure there has to be a better way, but in the meantime:
$array = array("a", "a", "a", "b", "b", "b", "b");
$result=array();
foreach ($array as $value) {
if (! in_array($value, $result)) {
array_push($result,$value);
} else {
array_push($result," ");
}
}
print_r($result);
This will get you something like an "array_unique" but replacing all repeated values with " " no matter the order of the values.
edit: If you need a function that replaces the original array you can use this:
$array = array("a", "a", "a", "c", "b", "b", "b", "b","c");
function array_replace_repeat(&$array) {
$tmpArr=$array;
$array=array();
foreach ($tmpArr as $value) {
if (! in_array($value, $array)) {
array_push($array,$value);
} else {
array_push($array," ");
}
}
}
array_replace_repeat($array);