This should work for you:
Here I first use array_slice() to get an array of all elements which are before the current element of the iteration, e.g.
iteration value | sliced array
-----------------------------------
sara | []
jorj | [sara]
Then I use this array with array_filter(), to only keep the values with are equal to the current iteration value, so I can tell how many of the same values are in the array before the current value.
Now I simply have to count() how many there are and if there are more than 1 we also print it in the output.
Code:
$arr = array('sara','jorj','sara','sara','jorj','eli','ana');
foreach($arr as $key => $name) {
$count = count(array_filter(array_slice($arr, 0, $key), function($v)use($name){
return $v == $name;
})) + 1;
echo $name . ($count > 1 ? " - $count" : "") . PHP_EOL;
}
output:
sara
jorj
sara - 2
sara - 3
jorj - 2
eli
ana