我幾乎是一個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>
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>';
}
目前,該代碼產生我需要的隨機結果,但不是故障。當我能夠讓它產生全套故障時,隨機馬的列表停止運行。我從來沒有能夠按照值(0-20 +)的順序排序故障。我發現使用array_combine的另一個答案,並認爲也許使用這個,但我需要的關鍵(我認爲 - 也許我真的不?)。
如果你想看到它的行動,這裏是鏈接:http://www.eecreates.com/randomizer/dhjc%20randomizer/它是一個多類隨機數發生器,所以在第一頁你把你想隨機化的類的數量,然後下一頁你輸入類名字和馬匹輸入。
最終產品我要爲會是這個樣子:
顯示跳級
- 一分錢(0故障| 0故障)
- 謝爾頓(0故障| 4個故障)
- 拉吉(4個故障)
- 倫納德(5個故障)
- 霍華德(8個故障)
- 艾米法拉福勒(8個故障)
- 貝爾納黛特(9個故障)
我也會喜歡它顯示第二組故障對於那些誰擁有零開始與馬,但當然一心一意。 :) 謝謝!