我正在制作一個小程式,將用戶輸入保存到陣列中,然后列印陣列,但我每次只列印最后一個輸入。我正在使用 readline 和 php.eol 因為它必須在 cmd 中作業。
<?php
echo "Hoeveel activiteiten wil je toevoegen?" . PHP_EOL;
$hoeveel = readline();
if (is_numeric($hoeveel)) {
for ($i = 1; $i <= $hoeveel; $i ) {
echo "Wat wil je toevoegen aan je bucketlist?" . PHP_EOL;
$entry *= $i;
$entry = readline() . PHP_EOL;
$bucketlist = array($entry, $entry, $entry);
}
echo "In jouw bucketlist staat: " . PHP_EOL;
foreach ($bucketlist as $key) {
echo $value;
}
} else {
exit($hoeveel . ' is geen getal, probeer het opnieuw');
}
?>
我已經在其中嘗試了一個關聯陣列,但似乎在普通陣列中應該是可能的。
uj5u.com熱心網友回復:
您可以將每個值存盤readline()到$bucketlist其中,然后回圈其中的值。
- 請注意,在您的代碼中沒有
echo $value; - 這部分可以洗掉
$entry *= $i;,因為它會立即被覆寫,而且還不存在
例子:
echo "Hoeveel activiteiten wil je toevoegen?" . PHP_EOL;
$hoeveel = readline();
$bucketlist = [];
if (is_numeric($hoeveel)) {
for ($i = 1; $i <= $hoeveel; $i ) {
echo "Wat wil je toevoegen aan je bucketlist?" . PHP_EOL;
$bucketlist[] = readline() . PHP_EOL;
}
echo "In jouw bucketlist staat: " . PHP_EOL;
foreach ($bucketlist as $value) {
echo $value;
}
} else {
exit($hoeveel . ' is geen getal, probeer het opnieuw');
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/446697.html
下一篇:用dplyr替換for回圈
