在PHP和打印中将HTML表单值存储到数组中

时间:2013-10-01 23:44:17

标签: php html arrays forms post

我试图在html文件中获取10个数字的用户输入,该文件将存储在php文件的数组中。虽然当我尝试打印阵列时,我得到的是“阵列”这个词,任何想法都是为什么?

HTML文件:

<!DOCTYPE HTML>
<html>
<head>
<meta charset= "utf-8">
</head>
<body>
Please enter 10 numbers that will be stored in an array:
<form action="practice3.php" method="post">
<input type="text" name="number1[]" placeholder="First number"><br />
<input type="text" name="number2[]" placeholder="Second number"><br />
<input type="text" name="number3[]" placeholder="Third number"><br />
<input type="text" name="number4[]" placeholder="Fourth number"><br />
<input type="text" name="number5[]" placeholder="Fifth number"><br />
<input type="text" name="number6[]" placeholder="Sixth number"><br />
<input type="text" name="number7[]" placeholder="Seventh number"><br />
<input type="text" name="number8[]" placeholder="Eighth number"><br />
<input type="text" name="number9[]" placeholder="Ninth number"><br />
<input type="text" name="number10[]" placeholder="Tenth number"><br />
<input type="submit" name="submit" value="Submit">
</form>
</body>

PHP文件:

<?php

    $number1 = $_POST['number1'];
    $number2 = $_POST['number2'];
    $number3 = $_POST['number3'];
    $number4 = $_POST['number4'];
    $number5 = $_POST['number5'];
    $number6 = $_POST['number6'];
    $number7 = $_POST['number7'];
    $number8 = $_POST['number8'];
    $number9 = $_POST['number9'];
    $number10 = $_POST['number10'];

    $myArray = Array($number1, $number2, $number3, $number4, $number5, $number6, $number7, $number8, $number9, $number10);

    echo $myArray;



?>

4 个答案:

答案 0 :(得分:5)

只需使用print_r

echo '<pre>';
print_r ($myArray);
echo '</pre>';

它将以人类可读的方式显示有关变量的信息。可以与数组和对象一起使用。

答案 1 :(得分:1)

print_r会有效,但如果您想控制格式化,则需要iterate数组

foreach ($myArray as $value) {
   echo "Value: $value<br />\n";
}

答案 2 :(得分:1)

只是详细说明,echo需要一个字符串,但不适用于Arrays。通常,在调试时,人们倾向于使用var_dump(),它适用于对象,数组,字符串等。

答案 3 :(得分:1)

您的输入名称中不需要[],number1,number2,...就足够了。然后只需使用print_r打印数组。