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);
Output for 5.6.38, 7.1.0 - 7.1.25, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.30, 8.2.0 - 8.2.25, 8.3.0 - 8.3.13
Array ( [0] => gold [1] => gold-2 [2] => blue night [3] => blue night-2 [4] => silver morning [5] => about earth [6] => about earth-2 [7] => about earth-3 [8] => sun vs king )

preferences:
77.14 ms | 408 KiB | 5 Q