1

I am trying to get user input of 10 numbers in an html file that will be stored into an array on a php file. Although when I try to print the array out all I am getting is the word "Array", any idea as to why?

HTML File:

<!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 File:

<?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 Answers 4

5

Just use print_r :

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

It will display information about your variable in a way that's readable by humans. can be used with arrays and objects.

Sign up to request clarification or add additional context in comments.

4 Comments

That worked great, but why am I getting this? Array ( [0] => Array ( [0] => 0 ) [1] => Array ( [0] => 9 ). I dont understand. Is it saying the first element of the first element of the array? There are a lot of first elements in the line
use echo '<pre>';. It will be more readable. see the update.
Is this making an array within each element of the array? I am seeing two [0] for each number?
Yes it does because you are naming your inputs with []. You have to remove it.
1

Just to elaborate, echo expects a string and doesn't work for Arrays. In general when debugging people tend to use var_dump () which works on Objects, Arrays, Strings, etc.

Comments

1

You don't need the [] in your input names, number1, number2, ... will suffice. Then just use print_r to print the array.

Comments

1

print_r will work but if you want control over formating you will need to iterate over the array

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

2 Comments

I tried it this way also. I am getting the word "Array" 10 times in a row. Closer, but the print_r way is working, but I would like control over the format
The update should now work once you remove [] from the HTML input names. I grabbed the wrong example from the PHP manual. I strongly suggest you read the PHP manual page I have linked to in my answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.