Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/235.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php循环输入带有随机值的数字_Php_Loops_Shuffle - Fatal编程技术网

php循环输入带有随机值的数字

php循环输入带有随机值的数字,php,loops,shuffle,Php,Loops,Shuffle,有人能帮我吗,我的剧本有问题 如果我在输入字段nmber中输入4324,我希望得到如下结果: 4324 4342 4234 4243 4432 4423 3424 3442 3244 2434 2443 2344 这是我的剧本: <form name="a" method="POST" action=""> <table border="1" width="100%">

有人能帮我吗,我的剧本有问题

如果我在输入字段nmber中输入4324,我希望得到如下结果:

    4324
    4342
    4234
    4243
    4432
    4423
    3424
    3442
    3244
    2434
    2443
    2344
这是我的剧本:

<form name="a" method="POST" action="">
    <table border="1" width="100%">
    <tbody><tr>
    <td height="38" align="center"><b>Number</b>&nbsp;&nbsp;
        <input name="nmber" size="8.5" maxlength="4"  type="text" value="<?php echo $_POST['nmber']; ?>">&nbsp;&nbsp;<b>Buy</b>&nbsp;&nbsp;
        <input name="buy" size="6" type="text" value="<?php echo $_POST['buy']; ?>">&nbsp;<font color="#000000" size="2"><b>(x 1000)</b></font>&nbsp;&nbsp;
        <input name="save" style="padding:7px;" value="Submit" type="submit">
    </td>
    </tr>
    </tbody></table>
</form>

您正试图使用字符串4324生成长度为4的所有排列。 生成所有置换(imho)的最简单方法是递归。但你们也可以用迭代法

我建议你先研究算法,掌握递归。谷歌快速搜索返回以下结果


    • 这就是您需要的:

      function getCombinations(array $a)
      {
      
        switch (TRUE)
        {
      
          case !isset($a[1]):
            return $a;
      
          case !isset($a[2]):
            return array(implode($a), implode(array_reverse($a)));
      
          default:
      
            $return = [];
      
            foreach ($a as $k => $e)
            {
      
              $c = $a;
      
              array_splice($c, $k, 1);
      
              foreach (getCombinations($c) as $r)
              {
                $return[] = $e . $r;
              }
      
            }
      
            return array_unique($return);
      
        }
      
      }
      
      $s = '4324';
      
      echo implode('<br>', getCombinations(str_split($s)));
      
      函数getCombinations(数组$a)
      {
      开关(真)
      {
      case!isset($a[1]):
      返回$a;
      case!isset($a[2]):
      返回数组(内爆($a),内爆(数组_反向($a));
      违约:
      $return=[];
      foreach($a为$k=>$e)
      {
      $c=$a;
      阵列拼接($c,$k,1);
      foreach(getcompositions($c)作为$r)
      {
      $return[]=$e.$r;
      }
      }
      返回数组_unique($return);
      }
      }
      $s='4324';
      回波内爆(“
      ”,getCombinations(str_split($s));
      它需要完全按照这个顺序,还是可以按照任何顺序?谢谢@MichaelRushton回答我的问题。。我要的是样本中的结果。好的,我下面的答案就是这样。
      function getCombinations(array $a)
      {
      
        switch (TRUE)
        {
      
          case !isset($a[1]):
            return $a;
      
          case !isset($a[2]):
            return array(implode($a), implode(array_reverse($a)));
      
          default:
      
            $return = [];
      
            foreach ($a as $k => $e)
            {
      
              $c = $a;
      
              array_splice($c, $k, 1);
      
              foreach (getCombinations($c) as $r)
              {
                $return[] = $e . $r;
              }
      
            }
      
            return array_unique($return);
      
        }
      
      }
      
      $s = '4324';
      
      echo implode('<br>', getCombinations(str_split($s)));