PHP: How to replace duplicate array values in an array with a fixed size

I have an array with a fixed size of 10. The array is filled with random numbers. What I want is to print the array with the size of 10 with all the duplicates filtered out.

I used array_unique, but it just removes the duplicates and does not replace the duplicate values with new random numbers. So, sometimes the size is 9 numbers and other times the size is 7 numbers. I want the array to be always 10 numbers long with no duplicate values. I already tried several functions (in_array, isset, array_replace, array_merge, array_search), but I have no idea how to accomplish what I want. I'm at the part where my code can detect that there are duplicates, but I have no idea how to replace those duplicates with new random numbers which are also unique. Here is the code I wrote:

$numbers = Array (rand(10,100), rand(10,100), rand(10,100), rand(10,100), rand(10,100), rand(10,100), rand(10,100), rand(10,100), rand(10,100), rand(10,100));
            If (count($numbers) > count(array_unique($numbers)))
                {
                    Foreach ($numbers As $number)
                    {
                       $inArray = array_search($number, $numbers);
                       If ($inArray === FALSE)
                            {
                               echo "No duplicate(s) found" ;
                            }
                       Else
                            {
                               echo "Duplicate(s) found" ;
                            }
                    }

                   
                }
            Else
                {      
                }

Answer

Solution:

An easy (but not optimal) solution would be to keep adding values to array while its length is not 10, and always remove the duplicates, this will ensure the array has 10 different random elements.

$array = [];

while (count($array) < 10){
    $array[] = rand(10, 100);
    $array = array_unique($array);
}

print_r($array);

Answer

Solution:

Maybe like this?

<?php
$numbers = [rand(10,100), rand(10,100), rand(10,100), rand(10,100), rand(10,100), rand(10,100), rand(10,100), rand(10,100), rand(10,100), rand(10,100)];

    foreach ($numbers as $key => $number)
    {
        if (in_array($number,$numbers))
        {
            $numbers[$key] = rand(10,100);
        }

    }


print_r($numbers);
?>

Answer

Solution:

For small array like this (100 numbers) you can generate an array with 10-100 elements then random pick.

One line solution:

print_r(array_rand(range(10, 100), 10));

Source