Php 从两个不同的阵列创建和显示信息?

Php 从两个不同的阵列创建和显示信息?,php,Php,我基本上是一个PHP初学者,虽然我可以像其他人一样复制和粘贴,而且通常我能够把它分离出来并弄清楚到底发生了什么:)这是我在这里的第一篇帖子,但我从其他答案中得到了很多帮助,所以感谢所有过去的帮助,希望未来的帮助 基本上,我想在这里复制的是一个马术表演跳跃项目,在这个项目中,每击倒一根杆子等于四次失误,时间失误是在允许的时间内给出的。所有跳开的马,也就是零失误的马,都会进入跳开回合,在那里他们可能会或可能不会出现失误 我当前的问题是使用for和foreach循环来推进两个不同的数组。我遇到的问题是

我基本上是一个PHP初学者,虽然我可以像其他人一样复制和粘贴,而且通常我能够把它分离出来并弄清楚到底发生了什么:)这是我在这里的第一篇帖子,但我从其他答案中得到了很多帮助,所以感谢所有过去的帮助,希望未来的帮助

基本上,我想在这里复制的是一个马术表演跳跃项目,在这个项目中,每击倒一根杆子等于四次失误,时间失误是在允许的时间内给出的。所有跳开的马,也就是零失误的马,都会进入跳开回合,在那里他们可能会或可能不会出现失误

我当前的问题是使用for和foreach循环来推进两个不同的数组。我遇到的问题是,如果一个数组工作,另一个数组就会停止。其中一个数组需要洗牌,另一个数组需要按数字顺序排序。基本上,这是一个随机发生器,它会把一系列的马,给他们排名,然后分配一个权重随机数错误(这是一个马游戏,书呆子,我知道)。以下是我正在使用的代码:

index.php

<form action="classes.php" method="post">
    How many classes do you wish to randomize?<br />
    <input type="text" name="number"><br />
    <input type="submit" /><br />
</form>

您希望随机分配多少类?


classes.php

<form action="action.php" method="post">
<?php
$number = $_POST['number']; //This is the desired value of Looping
echo '<input type="hidden" value="' . $number . '" name="number">';
$i = 1; //First we set the count to be zero
while ($i<=$number) {
    echo '

    Class: <input type="text" name="class' . $i . '"><br />
    <textarea cols="100" rows="20" name="entries' . $i . '"></textarea><br />
    <br />';
    $i++; //Increase the value of the count by 1
};
?>
<input type="submit" /><br />
</form>
<form action="action.php" method="post">
<?php
 $number = $_POST['number']; //This is the desired value of Looping
 echo '<input type="hidden" value="' . $number . '" name="number">';
 $i = 1; //First we set the count to be zero
 while ($i<=$number) {
   echo '

   Class: <input type="text" name="class[]"><br />
   <textarea cols="100" rows="20" name="entries[]"></textarea><br />
   <br />';
   $i++; //Increase the value of the count by 1
 };
?>

<input type="submit" /><br />
</form>


action.php

$number = $_POST['number']; //This is the desired value of Looping

function array_rand_weighted($values) {
    $r = mt_rand(1, array_sum($values));
    foreach ($values as $item => $weight) {
        if  ($r <= $weight) return $item;
        $r -= $weight;
    }
}

for ($i=1; $i<=$number; $i++) {
    $class[$i] = $_POST['class'.$i];

    //trim off excess whitespace off the whole
    $text[$i] = trim($_POST['entries'.$i]);

    //explode all separate lines into an array
    $textAr[$i] = explode("\n", $text[$i]);

    //trim all lines contained in the array.
    $textAr[$i] = array_filter($textAr[$i], 'trim');

    //shuffle the results
    shuffle($textAr[$i]);

    //add faults
    //loop through the lines
    echo '<div id="output">
    [b]' . $class[$i] . '[/b]<br />'; 
    foreach($textAr[$i] as $key[$i]=>$line[$i]){
        $knockdowns = array( 0 => 20, 1 => 25, 2 => 30, 3 => 10, 4 => 8, 5 => 7); // knockdowns
        $knockdowns = array_rand_weighted($knockdowns)*4;
        $timefaults = array( 0 => 75, 1 => 10, 2 => 10, 3 => 5); // time faults
        $timefaults = array_rand_weighted($timefaults);

        $faultsadded = $knockdowns + $timefaults;

        $faults[$i] = $faultsadded;
    }

    asort($faults);

    foreach($textAr[$i] as $key[$i]=>$line[$i]){
        echo $key + 1,". " . $line . " (" . $faults[$i] . " Faults)<br />"; 
    }
    echo '</div>';
}
$number=$\u POST['number']//这是循环的期望值
函数数组\u rand\u加权($value){
$r=mt_rand(1,数组和($value));
foreach($item=>$weight的值){
如果($r$行[$i]){
$knockdowns=array(0=>20,1=>25,2=>30,3=>10,4=>8,5=>7);//knockdowns
$knockdowns=数组\随机数\加权($knockdowns)*4;
$timefaults=array(0=>75,1=>10,2=>10,3=>5);//时间错误
$timefaults=阵列随机加权($timefaults);
$faultsadded=$knockdowns+$timefaults;
$faults[$i]=$faultsadded;
}
asort($故障);
foreach($textAr[$i]作为$key[$i]=>$line[$i]){
回显$key+1,“.”行“(“$faults[$i]”故障)
”; } 回声'; }
目前,此代码正在生成我需要的随机结果,但不是错误。当我能够让它生成全套错误时,随机马列表将停止运行。我从未能够让错误按值(0-20+)的顺序排序.我用array_combine找到了另一个答案,我想也许可以用这个,但我需要钥匙(我想-也许我真的不需要?)

如果你想看到它的作用,这里有一个链接:它是一个多类随机化器,所以在第一页你把你想随机化的类的数量,然后在下一页你输入类名和输入的马

我想要的最终产品是这样的:

表演跳跃课

  • penny(0个故障| 0个故障)
  • 谢尔顿(0个断层| 4个断层)
  • raj(4个断层)
  • 伦纳德(5次失误)
  • 霍华德(8次失误)
  • 艾米·法拉·福勒(8次失误)
  • 伯纳黛特(9处断层)

  • 我还想让它为那些一开始就没有错误的马匹显示第二组错误,但当然一次只能显示一件事:)谢谢!

    全新的答案。index.php保持不变。我重新编写了classes.php中的代码以使用命名数组。这简化了action.php中的处理。请参阅下面的注释。代码这里将需要包装在一个合适的HTML页面布局

    请注意,虽然代码的更改非常广泛,但我已尽量减少了对数据结构的更改。如果我从头开始,这不一定是我处理数据结构的方式

    classes.php

    <form action="action.php" method="post">
    <?php
    $number = $_POST['number']; //This is the desired value of Looping
    echo '<input type="hidden" value="' . $number . '" name="number">';
    $i = 1; //First we set the count to be zero
    while ($i<=$number) {
        echo '
    
        Class: <input type="text" name="class' . $i . '"><br />
        <textarea cols="100" rows="20" name="entries' . $i . '"></textarea><br />
        <br />';
        $i++; //Increase the value of the count by 1
    };
    ?>
    <input type="submit" /><br />
    </form>
    
    <form action="action.php" method="post">
    <?php
     $number = $_POST['number']; //This is the desired value of Looping
     echo '<input type="hidden" value="' . $number . '" name="number">';
     $i = 1; //First we set the count to be zero
     while ($i<=$number) {
       echo '
    
       Class: <input type="text" name="class[]"><br />
       <textarea cols="100" rows="20" name="entries[]"></textarea><br />
       <br />';
       $i++; //Increase the value of the count by 1
     };
    ?>
    
    <input type="submit" /><br />
    </form>
    
    
    
    action.php

    $number = $_POST['number']; //This is the desired value of Looping
    
    function array_rand_weighted($values) {
        $r = mt_rand(1, array_sum($values));
        foreach ($values as $item => $weight) {
            if  ($r <= $weight) return $item;
            $r -= $weight;
        }
    }
    
    for ($i=1; $i<=$number; $i++) {
        $class[$i] = $_POST['class'.$i];
    
        //trim off excess whitespace off the whole
        $text[$i] = trim($_POST['entries'.$i]);
    
        //explode all separate lines into an array
        $textAr[$i] = explode("\n", $text[$i]);
    
        //trim all lines contained in the array.
        $textAr[$i] = array_filter($textAr[$i], 'trim');
    
        //shuffle the results
        shuffle($textAr[$i]);
    
        //add faults
        //loop through the lines
        echo '<div id="output">
        [b]' . $class[$i] . '[/b]<br />'; 
        foreach($textAr[$i] as $key[$i]=>$line[$i]){
            $knockdowns = array( 0 => 20, 1 => 25, 2 => 30, 3 => 10, 4 => 8, 5 => 7); // knockdowns
            $knockdowns = array_rand_weighted($knockdowns)*4;
            $timefaults = array( 0 => 75, 1 => 10, 2 => 10, 3 => 5); // time faults
            $timefaults = array_rand_weighted($timefaults);
    
            $faultsadded = $knockdowns + $timefaults;
    
            $faults[$i] = $faultsadded;
        }
    
        asort($faults);
    
        foreach($textAr[$i] as $key[$i]=>$line[$i]){
            echo $key + 1,". " . $line . " (" . $faults[$i] . " Faults)<br />"; 
        }
        echo '</div>';
    }
    
    我已经将加权随机故障代码移到了它自己的函数中。由于来自classes.php的数据现在已经是数组形式,我简化了处理,并将其与输出分离。一旦处理完成并生成了故障数,一对嵌套循环将按类和进入者生成输出

    <?php
    
    function array_rand_weighted($values) {
        $r = mt_rand(1, array_sum($values));
        foreach ($values as $item => $weight) {
            if  ($r <= $weight) return $item;
            $r -= $weight;
        }
    }
    
    // generate a random value for faults and return in sorted order, ascending.
    function generateFaults($numEntries) {
    
    $faults = array();
      while ($numEntries) {
      $knockdowns = array( 0 => 20, 1 => 25, 2 => 30, 3 => 10, 4 => 8, 5 => 7); // knockdowns
      $knockdowns = array_rand_weighted($knockdowns)*4;
      $timefaults = array( 0 => 75, 1 => 10, 2 => 10, 3 => 5); // time faults
      $timefaults = array_rand_weighted($timefaults);
      $faults[] = $knockdowns + $timefaults;
      $numEntries--;
    }
    //  echo nl2br(print_r($faults, true));
    sort($faults);
    return $faults;
    }
    
    $textAr = array();
    $faults = array();
    // Iterate over the entries array, extracting the names of our entrants.
    foreach ( $_POST['entries'] as $entries) {
    
        //explode all separate lines into an array
      $tempEntries = explode("\n", $entries);
      //shuffle the results
      shuffle($tempEntries);
    
      //trim all lines contained in the array and assign to next entry in $textAr
      $textAr[] = array_filter($tempEntries, 'trim');
      //add faults
      $faults[] = generateFaults(count($tempEntries));
    }
    //loop through the lines
    //  echo nl2br(print_r($faults, true));
    
    for ($i=0; $i<count($_POST['class']); $i++) {
      echo '<div id="output">
      <b>' . $_POST['class'][$i] . '</b><br />'; 
    
      for($j = 0; $j<count($textAr[$i]); $j++) {
      echo $j + 1,". " . $textAr[$i][$j] . " (" . $faults[$i][$j] . " Faults)<br />"; 
    }
    echo '</div>';
    }
    ?>
    
    
    
    脚注: