[英]html form option value as part of $_POST or $_GET multidimensional array?
[英]Post multidimensional array HTML form
我嘗試得到一個像這樣的 PHP 數組
array(
color => array("blue", "red", "yellow", "pink"),
size => array("small", "medium, "large"),
width => array("30", "32, "34"),
);
數據來自 HTML 表格,如下所示
<form method="post" action="post.php">
<input type="text" name="data[color][]" value="red"/>
<input type="text" name="data[color][]" value="blue"/>
<input type="text" name="data[size][]" value="small"/>
<input type="text" name="data[width][]" value="30"/>
<input type="text" name="data[width][]" value="32"/>
</form>
問題是只有最后一個項目發布在數組中。 陣列中的 output 是
array(
color => array("blue"),
size => array("small"),
width => array("32"),
);
我的表格中是否缺少某些內容,為什么會發生這種情況?
您沒有提交您的 POST 添加提交按鈕 - 試試這個例如:
<form method="post" action="">
<input type="text" name="data[color][]" value="red"/>
<input type="text" name="data[color][]" value="blue"/>
<input type="text" name="data[size][]" value="small"/>
<input type="text" name="data[width][]" value="30"/>
<input type="text" name="data[width][]" value="32"/>
<input type="submit" />
</form>
<?php
echo '<pre>';
print_r($_POST); // this will only print a filled array once submit is clicked.
如果沒有點擊提交,你會看到這個:
Array
(
)
如果是這樣的話:
Array
(
[data] => Array
(
[color] => Array
(
[0] => red
[1] => blue
)
[size] => Array
(
[0] => small
)
[width] => Array
(
[0] => 30
[1] => 32
)
)
)
在您的 post.php 文件上 - 您可能正在打印一個完全不同的數組,因為如果不提交表單,此數據將保留在輸入行中......您應該打印: $_POST; 就像在這個例子中一樣。
如果沒有提交按鈕,您甚至是如何到達那里的,這超出了我的范圍... :)
我仍然不確定你想要什么,但是這個怎么樣:
foreach($_POST["data"] as $row => $data)
{
echo("$row = " . implode(", ", $data) . PHP_EOL);
}
將產生:
color = red, blue
size = small
width = 30, 32
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.