简体   繁体   English

如何使用 php 从数字范围内随机用“-”替换数字

[英]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.我想随机替换这个范围内的 2 个数字。

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:在 1 到 10 的范围内创建一个数组,选择 2 个随机键并将它们替换为所需的字符串:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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