4

I need to make app with will fill array with some random values, but if in array are duplicates my app not working correctly. So I need to write script code which will find duplicates and replace them with some other values. Okay so for example i have an array:

<?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;
}
?>

So result shall be the same array with replaced duplicated values.

3
  • 1
    You might want to check out array_unique() and array_replace() before needing to write your own method first; they might be just what you need. Commented Nov 4, 2012 at 3:03
  • I check that and it don't pass my needs. Thanks for trying to help me. Commented Nov 4, 2012 at 3:25
  • This question would be more clear if you expressed the replacement string(s) that you would like to use and your exact expected result. A minimal reproducible example @xZero Commented Aug 16, 2020 at 12:57

4 Answers 4

3

You can just keep track of the words that you've seen so far and replace as you go.

// 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;
    }
}

For a somewhat-optimized solution (for cases where there are not that many duplicates), use array_count_values() (reference here) to count the number of times it shows up.

// 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;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

2

Here the example how to generate unique values and replace recurring values in array

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');

Full example you can see here webbystep.ru

Comments

1

Use the function array_unique()

See more info at http://php.net/manual/en/function.array-unique.php

2 Comments

Thans for trying to help me. But it just removes duplicates. I wanna replace duplicates instead of removing it.
This will potentially reduce the size of the array -- the OP doesn't want this.
-1
$uniques = array();
foreach ($charset as $value) 
   $uniques[$value] = true;
$charset = array_flip($uniques);

2 Comments

Thanks. But how to use it? For example where to specify with which values duplicates will be replaced?
This will potentially reduce the size of the array -- the OP doesn't want this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.