I did something really cool with a small complexity O(2n) compared to your two loops O(n*n), so really quicker ! ! which is very important if you handle big big arrays
but i change a little bit what you need, let's say there is 3 occurences of "a" then in the result you'll have
a_1
a_2
a_3
and if there is 1 occurence of b
b_1
I don't think it's embarrassing for your needs
The script :
$arr[0] = "a";
$arr[1] = "b";
$arr[2] = "c";
$arr[3] = "a";
$arr[4] = "a";
$arr[5] = "b";
$arr[6] = "a";
$arr[7] = "d";
$arr[8] = "a";
$arr[9] = "b";
$arr[10] = "e";
for ($i = 0,$n = count($arr); $i < $n; $i++) {
$uTab[$arr[$i]] += 1;
$arr2[$i] = $arr[$i] . "_" . $uTab[$arr[$i]];
}
for ($i = 0,$n = count($arr2); $i < $n; $i++) {
echo "<br>" . $arr2[$i];
}
The result is
a_1
b_1
c_1
a_2
a_3
b_2
a_4
d_1
a_5
b_3
e_1