- array_count_values: documentation ( source)
- print_r: documentation ( source)
<?php
$title = ['gold','blue night','silver morning','about earth','sun vs king','blue night','about earth','gold','about earth'];
$value_counts = array_count_values($title);
#print_r($value_counts);
$expected_result = [];
foreach($value_counts as $k=>$v){
if($v<2){
$expected_result[] = $k;
}else{
$expected_result[] = $k;
for($i=2;$i<=$v;$i++){
$expected_result[] = $k."-".$i;
}
}
}
print_r($expected_result);