我假设您需要在数组中的每个字符串之后附加 -1, -2... 如果它不止一次出现。检查此代码:
<?php
$array = array("this", "is", "my", "string", "and", "it", "is", "a", "string", "do", "you", "like", "my", "string");
$ocurrences = array();
$iterator = new ArrayIterator($array);
while ($iterator->valid()) {
$keys = array_keys($ocurrences);
if (in_array($iterator->current(), $keys)) {
$array[$iterator->key()] = $iterator->current() . '-' . $ocurrences[$iterator->current()];
$ocurrences[$iterator->current()]++;
}
else {
$ocurrences[$iterator->current()] = 1;
}
$iterator->next();
}
print_r($array);
它将打印:
Array
(
[0] => this
[1] => is
[2] => my
[3] => string
[4] => and
[5] => it
[6] => is-1
[7] => a
[8] => string-1
[9] => do
[10] => you
[11] => like
[12] => my-1
[13] => string-2
)