php PHP从表单输入字段创建数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12533044/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP creating an array from form input fields
提问by Lonergan6275
I'm trying to create an array to store numbers and add them together.
我正在尝试创建一个数组来存储数字并将它们加在一起。
<input type="number" name="numbers[]"/>
I'm getting an undefined variable on the following line
我在下一行得到一个未定义的变量
foreach($numbers as $number)
I'm sure this is probably something basic but I'm relatively new to php and help would be greatly appreciated.
我确定这可能是一些基本的东西,但我对 php 比较陌生,非常感谢您的帮助。
回答by thewebguy
If you posted the inputs you're showing from one page to another and you need to run through the list you should set it up like this:
如果您发布了input从一页显示到另一页的s 并且您需要遍历列表,则应该像这样设置它:
if (isset($_REQUEST['numbers']) && is_array($_REQUEST['numbers'])) {
$numbers = $_REQUEST['numbers'];
foreach ($numbers as $number) {
print $number;
}
}
回答by Baba
If you want to get the sum of an array you dont need to loop you can use array_sum
如果你想得到一个不需要循环的数组的总和,你可以使用 array_sum
Example
例子
<?php
if (isset($_POST['numbers'])) {
echo array_sum($_POST['numbers']);
}
?>
<form method="POST">
<input type="number" name="numbers[]"/>
<input type="number" name="numbers[]"/>
<input type="number" name="numbers[]"/>
<input type="number" name="numbers[]"/>
<input type="submit" value="add"/>
</form>
回答by qrazi
Add some more code, but it basically means that $numbers is not declared yet at this point in the code.
添加更多代码,但这基本上意味着此时代码中还没有声明 $numbers 。
Add this line before:
在此之前添加此行:
$numbers = array();
Now it shouldnt give you this error.
现在它不应该给你这个错误。
So now the question is, where is $numbers supposed to be set? For that we need more info and code.
那么现在的问题是,$numbers 应该在哪里设置?为此,我们需要更多信息和代码。
回答by ChapmIndustries
I'm no PHP expert but if I understand you correctly and you are trying to put a variable in an array you would need to say what part of the Array I would assume. I'm pretty sure that $array[i]and $arrayare not the same and that if you try to put something into $arrayit will only be able to hold one thing at a time. If you are just gathering a bunch of numbers that someone enters from a form then you could do something like this.
我不是 PHP 专家,但如果我理解正确并且您尝试将变量放入数组中,则需要说明我假设数组的哪一部分。我很确定$array[i]和$array不一样,如果你试图把东西放进去,$array它一次只能容纳一件东西。如果您只是收集某人从表单中输入的一堆数字,那么您可以执行以下操作。
//For the form
<form action="formHandler.php" method="post">
<input type="text" name="number1">
<input type="text" name="number2">
//continue in this manner for as many numbers as you need to gather.
</form>
//for the PHP side of it.
<?php
$x=1;
$numbersArray = array();
for($i=0; $i<10; $i++){
$numbersArray[$i] = $_POST['number'.$x];
$x++;
}
//to add them up
$total=array_sum($numbersArray);
?>
I hope that's right and I hope it helps.
我希望这是正确的,我希望它有所帮助。

