3 回答

TA貢獻1891條經驗 獲得超3個贊
你做對了。您只需要在 foreach 循環之外打印項目值。在循環內它總是打印最后一個值
<?php
if (isset($_POST['sub'])) {
$no= $_POST['no'];
// $ex = explode(",", $no);
$items = array ();
foreach ($no as $item) {
echo $item; // Do something with item
$items[] = $item;
}
print_r($items);
}
?>

TA貢獻1786條經驗 獲得超11個贊
當您使用此模式no[]時,您將發送一個數組,因此無需使用爆炸。
該行也不$item = array ();執行任何操作,因為您正在填寫 $itemforeach。
因此,更改您的代碼如下:
<body>
<form action="array_check.php" method="post">
<input type="number" name="no[]">
<input type="number" name="no[]">
<input type="number" name="no[]">
<input type="submit" name="sub">
</form>
</body>
</html>
<?php
if (isset($_POST['sub'])) {
$no = $_POST['no'];
foreach ($no as $item) {
echo $item ; // Do something with item
print_r($item);
}
}

TA貢獻1995條經驗 獲得超2個贊
這應該可以做到:
<?php
if (isset($_POST['sub'])) {
$no= $_POST['no'];
$nos = explode(",", $no);
$items = array (); // careful here you are using $item twice !
foreach ($nos as $no) {
$items[] = $no;
}
var_dump($items);
}
?>
- 3 回答
- 0 關注
- 241 瀏覽
添加回答
舉報