1

我正在尝试在 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

4 回答 4

5

只需使用print_r

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

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

于 2013-10-01T23:47:52.173 回答
1

print_r会工作,但如果你想控制格式化,你需要遍历数组

foreach ($myArray as $value) {
   echo "Value: $value<br />\n";
}
于 2013-10-01T23:49:57.803 回答
1

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

于 2013-10-01T23:50:15.553 回答
1

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

于 2013-10-01T23:53:53.200 回答