将用户输入添加到数组
我正在尝试使用 array_merge 将用户输入添加到先前创建的数组中。但是,我无法将整个新数组作为无序列表进行回显。用户的输入正在被正确处理,但原始数组在无序列表中显示为“数组”。这是代码:
<?php
$travel = array("Automobile", "Jet", "Ferry", "Subway");
foreach ($travel as $t)
{
echo "<ul>";
echo "<li>$t</li>";
echo "</ul>";
}
?>
<form action="arrays.php" method="post">
<input type="text" name="added" />
<?php
foreach ($travel as $t)
{
echo "<input type=\"text\" name=\"travel[]\" value=\"$t\" />";
}
?>
<input type="submit" name="submit" value="Add More!" />
</form>
<?php
$travel = array($_POST["travel"]);
$added = explode(",", $_POST["added"]);
$travel = array_merge($travel, $added);
echo "<p> Here is the list with your additions:</p>";
echo "<ul>";
foreach ($travel as $t)
{
echo "<li>$t</li>";
}
echo "</ul>";
?>
I am trying to add user input to a previously created array with array_merge. However, I am having trouble echoing the entire, new array as an unordered list. the user's entry is being processed correctly, but the original array is displaying as "Array" within the unordered list. Here is the code:
<?php
$travel = array("Automobile", "Jet", "Ferry", "Subway");
foreach ($travel as $t)
{
echo "<ul>";
echo "<li>$t</li>";
echo "</ul>";
}
?>
<form action="arrays.php" method="post">
<input type="text" name="added" />
<?php
foreach ($travel as $t)
{
echo "<input type=\"text\" name=\"travel[]\" value=\"$t\" />";
}
?>
<input type="submit" name="submit" value="Add More!" />
</form>
<?php
$travel = array($_POST["travel"]);
$added = explode(",", $_POST["added"]);
$travel = array_merge($travel, $added);
echo "<p> Here is the list with your additions:</p>";
echo "<ul>";
foreach ($travel as $t)
{
echo "<li>$t</li>";
}
echo "</ul>";
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试为 array_merge() 创建的新数组使用新的变量名称。我认为您在修改要存储的数组时可能会遇到问题。
Try using a new variable name for the new array created by array_merge(). I think you may run into problems modifying the array you're storing into.
应该是
should be
问题是这样解决的:
The problem was solved thus: