3v4l.org

run code in 300+ PHP versions simultaneously
<?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);

preferences:
61.57 ms | 405 KiB | 5 Q