简体   繁体   中英

How replace number with “-” randomly from a range of number using php

This is my sample data. i want to replace 2 number from this range randomly.

1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
1, 2, 3, 4, 5, 6, 7, 8, 9, 10,

i trying this code.

<?php
for ($i=1; $i <= $noof_q; $i++){
    for ($j=0; $j < $max_range; $j+=$increment){
        echo $min_range+$j.", ";
    }
    if ($j==$max_range) {
         echo "<br>";
    }
}
?>

hope, this code help you

$firstRand = [rand(1, 10), rand(1, 10)];
$secondRand = [rand(1, 10), rand(1, 10)];

for ($i=1; $i < 11; $i++) {
    for ($j=1; $j < 11; $j++) {
        $myNumber = $j;

        if (
            $i === $firstRand[0] && $j === $firstRand[1] || 
            $i === $secondRand[0] && $j === $secondRand[1]
        ) {
            $myNumber = '-';
        }

        echo $myNumber . ", ";

        if ($j == 10) {
            echo "<br>";
        }
    }
}

Create an array in the range from 1 to 10, pick 2 random keys and replace them with the desired string:

<?php
$replaceString = '--';
for($x=1; $x<=5; $x++) {
    $range = range(1,10);
    $randomKeys = array_rand($range, 2);
    $range[$randomKeys[0]] = $replaceString;
    $range[$randomKeys[1]] = $replaceString;
    echo implode(',', $range);
    echo '<br>';
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM