Php 查找并替换阵列中的重复项

Php 查找并替换阵列中的重复项,php,arrays,replace,find,duplicates,Php,Arrays,Replace,Find,Duplicates,我需要使应用程序与将填充一些随机值数组,但如果在数组中重复我的应用程序不能正常工作。因此,我需要编写脚本代码,找到重复项并用其他值替换它们。 例如,我有一个数组: <?PHP $charset=array(123,78111,0000,123,900,134,00000,900); function arrayDupFindAndReplace($array){ // if in array are duplicated values then -> Replace duplic

我需要使应用程序与将填充一些随机值数组,但如果在数组中重复我的应用程序不能正常工作。因此,我需要编写脚本代码,找到重复项并用其他值替换它们。 例如,我有一个数组:

<?PHP
$charset=array(123,78111,0000,123,900,134,00000,900);

function arrayDupFindAndReplace($array){

// if in array are duplicated values then -> Replace duplicates with some other numbers which ones I'm able to specify.
return $ArrayWithReplacedValues;
}
?>
使用该函数
数组_unique()


查看更多信息,请访问

,您只需跟踪到目前为止看到的单词,并在移动过程中替换即可

// words we've seen so far
$words_so_far = array();
// for each word, check if we've encountered it so far
//    - if not, add it to our list
//    - if yes, replace it
foreach($charset as $k => $word){
    if(in_array($word, $words_so_far)){
        $charset[$k] = $your_replacement_here;
    }
    else {
        $words_so_far[] = $word;
    }
}
对于某种程度上优化的解决方案(对于没有那么多重复项的情况),请使用array_count_values()来计算它显示的次数

// counts the number of words
$word_count = array_count_values($charset);
// words we've seen so far
$words_so_far = array();
// for each word, check if we've encountered it so far
//    - if not, add it to our list
//    - if yes, replace it
foreach($charset as $k => $word){
    if($word_count[$word] > 1 && in_array($word, $words_so_far)){
        $charset[$k] = $your_replacement_here;
    }
    elseif($word_count[$word] > 1){
        $words_so_far[] = $word;
    }
}

下面的示例演示如何生成唯一值并替换数组中的重复值

function get_unique_val($val, $arr) {
    if ( in_array($val, $arr) ) {
        $d = 2; // initial prefix 
        preg_match("~_([\d])$~", $val, $matches); // check if value has prefix
        $d = $matches ? (int)$matches[1]+1 : $d;  // increment prefix if exists

        preg_match("~(.*)_[\d]$~", $val, $matches);

        $newval = (in_array($val, $arr)) ? get_unique_val($matches ? $matches[1].'_'.$d : $val.'_'.$d, $arr) : $val;
        return $newval;
    } else {
        return $val;
    }
}

function unique_arr($arr) {
    $_arr = array();
    foreach ( $arr as $k => $v ) {
        $arr[$k] = get_unique_val($v, $_arr);
        $_arr[$k] = $arr[$k];
    }
    unset($_arr);

    return $arr;
}




$ini_arr = array('dd', 'ss', 'ff', 'nn', 'dd', 'ff', 'vv', 'dd');

$res_arr = unique_arr($ini_arr); //array('dd', 'ss', 'ff', 'nn', 'dd_2', 'ff_2', 'vv', 'dd_3');

您可以看到的完整示例

您可能想签出,然后需要先编写自己的方法;它们可能正是你所需要的。我检查了一下,它没有满足我的需要。感谢您尝试帮助我。如果您表达了您想要使用的替换字符串和确切的预期结果,这个问题会更清楚。谢谢。但是如何使用它呢?例如,在何处指定将用哪些值替换重复项?这可能会减少数组的大小--OP不希望使用此.than来帮助我。但它只是删除重复项。我想替换重复项而不是删除它。这可能会减少数组的大小——OP不希望这样。
function get_unique_val($val, $arr) {
    if ( in_array($val, $arr) ) {
        $d = 2; // initial prefix 
        preg_match("~_([\d])$~", $val, $matches); // check if value has prefix
        $d = $matches ? (int)$matches[1]+1 : $d;  // increment prefix if exists

        preg_match("~(.*)_[\d]$~", $val, $matches);

        $newval = (in_array($val, $arr)) ? get_unique_val($matches ? $matches[1].'_'.$d : $val.'_'.$d, $arr) : $val;
        return $newval;
    } else {
        return $val;
    }
}

function unique_arr($arr) {
    $_arr = array();
    foreach ( $arr as $k => $v ) {
        $arr[$k] = get_unique_val($v, $_arr);
        $_arr[$k] = $arr[$k];
    }
    unset($_arr);

    return $arr;
}




$ini_arr = array('dd', 'ss', 'ff', 'nn', 'dd', 'ff', 'vv', 'dd');

$res_arr = unique_arr($ini_arr); //array('dd', 'ss', 'ff', 'nn', 'dd_2', 'ff_2', 'vv', 'dd_3');