Trying to learn arrays in PHP. Snippets posted for brevity.
HTML form here:
<p>What are your favorite type of cookies?</p>
<input type="checkbox" name="cookies[]" value="Oreos" />Oreos<br />
<input type="checkbox" name="cookies[]" value="Chocolate chip" />Chocolate chip<br />
<input type="checkbox" name="cookies[]" value="Sugar" />Sugar<br />
<input type="checkbox" name="cookies[]" value="Vanilla Mocha" />Vanilla Mocha<br />
<p>What are your favorite type of drinks?</p>
<input type="checkbox" name="drinks[]" value="Soda" />Soda<br />
<input type="checkbox" name="drinks[]" value="Wine" />Wine<br />
<input type="checkbox" name="drinks[]" value="Milk" />Milk<br />
<input type="checkbox" name="drinks[]" value="Water" />Water<br />
PHP page here:
foreach ($drinks as $d) {
echo "Your favorite drink(s) are: " . $d . "<br />";
}
foreach ($cookies as $cookie) {
echo "Your favorite cookies are: " . $cookie . "<br />";
}
$experimentalArray = array($cookie => $d);
foreach ($experimentalArray as $key => $value) {
echo "Cookie - " . $key . " Drink - " . $value . "<br /><br />";
}
Both cookies and drinks are multi-choice questions, so you can select more than one answer.
However, the experimentalArray only shows the last answer chosen in both drink and cookie question.
For example, I choose Oreos and Chocolate Chip in cookies, and Soda and Wine in drinks.
The answer comes out as: "Cookie - Chocolate chip Drink - Wine"
Why is it not displaying all values?
Edited for a multi-dimensional script
<?php
$drinks = $_POST['drinks'];
$cookies = $_POST['cookies'];
$combinedArray = array( 'Cookies' => $cookies, 'Drinks' => $drinks);
foreach($combinedArray as $snackType => $snack) {
print "<h2>$snackType</h2>";
foreach ($snack as $number => $snackChosen) {
print " $number is $snackChosen<br />";
}
} ?>
Ok, so tried to do a multi-dimensional array script instead since the previous script wasn't going to obtain all the values as per the HTML form.
This script works (was ripped off from a book and modified for this code here), however, $number value starts at 0. How do I modify that so that it starts at 1 instead?
Also, is this proper form for doing multi-dimensional array? Could it have been rewritten in a better way?
And again, thank you for all responses. Even if I don't quite understand them! :) So, thank you for your patience as well.
print_r($_POST);. Also, please, please tell me that you haveregister_globalsoff and you're just aliasing the $_POST array.