将用户输入添加到数组

发布于 2024-11-28 11:29:23 字数 913 浏览 0 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

鹿! 2024-12-05 11:29:23

尝试为 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.

迟月 2024-12-05 11:29:23
$travel = array($_POST["travel"]);

应该是

$travel = $_POST['travel'];
$travel = array($_POST["travel"]);

should be

$travel = $_POST['travel'];
那一片橙海, 2024-12-05 11:29:23

问题是这样解决的:

if (isset($_POST["submit"]))
{
$travel = $_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>";
}
?>

The problem was solved thus:

if (isset($_POST["submit"]))
{
$travel = $_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>";
}
?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文