2
<?php

$sessionTotal = 10;

        for($initial = 1; $initial <= $sessionTotal ; $initial++){
            echo '<input type="text" name="menuItems" size="20" /><br /><br/>';
        }

    //I have a if statement here checking if the submit button isset, yada yada, after I press the submit button, it returns this error -> Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\yada\yada-yada.php on line 43

    foreach($_POST['menuItems'] as $value)
    {
    echo $value;
    }

?>

It will echo $value after it is submitted. I have a if statement checking if the submit button isset, yada yada, after I press the submit button, it returns this error -> Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\yada\yada-yada.php on line 43

2
  • your post dont habe a menuItems entry! post the output of print_r($_POST); Commented Mar 5, 2012 at 9:15
  • or $_POST['menuItems'] is not an array.. Commented Mar 5, 2012 at 9:17

2 Answers 2

3

$_POST['menuItems'] is not an array, foreach only accepts arrays and certain objects.

If you make it

<?php

$sessionTotal = 10;

        for($initial = 1; $initial <= $sessionTotal ; $initial++){
            echo '<input type="text" name="menuItems[]" size="20" /><br /><br/>';
        }

    //I have a if statement here checking if the submit button isset, yada yada, after I press the submit button, it returns this error -> Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\yada\yada-yada.php on line 43

    if ( is_array( $_POST['menuItems'] ) ) 
    foreach($_POST['menuItems'] as $value)
    {
    echo $value;
    }

?>

It should work.

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

3 Comments

Cool, thanks, man. It works now, why does [] make it an array? I'll accept your answer in 8 minutes, it wouldn't allow me to accept before 8 minutes
-1 for not explaining why exactly those [] are needed. With your answer the OP may have a working example, but still hasn't got the first clue about why this happened in the first place.
@MouseHello because that's how array's are designated: variable[key], if you don't put a a key ,it creates a new value in the array.
0

There is nothing wrong with your foreach. There is something wrong with your understanding of how PHP parses input-attributes (_POST, _GET).


<input type="text" name="foobar" value="one">
<input type="text" name="foobar" value="two">
<input type="text" name="foobar" value="three">

translates to the application/x-www-form-urlencoded representation foobar=one&foobar=two&foobar=three.

PHP parses this string into a map (associative array). It does this somewhat like the following code:

<?php
$_GET = array();
$string = 'foobar=one&foobar=two&foobar=three';
$parts = explode('&', $string);
foreach ($parts as $part) {
    $p = explode('=', $part);
    $_GET[urldecode($p[0])] = urldecode($p[1]);
}

So basically it is assigning $_GET['foobar'] three times, leaving $_GET['foobar'] === 'three'.

Translated, this is what is happening here:

$_GET['foobar'] = 'one';
$_GET['foobar'] = 'two';
$_GET['foobar'] = 'three';

At this point I'd like to note that other languages (Ruby, Java, …) deal with this quite differently. Ruby for example recognizes the repeating key and builds something similar to $_GET['foobar'] = array('one', 'two', 'three').


There is a simple "trick" to tell PHP that the repeating value should be parsed into an array:

<input type="text" name="foobar[]" value="one">
<input type="text" name="foobar[]" value="two">
<input type="text" name="foobar[]" value="three">

will lead to $_GET['foobar'] = array('one', 'two', 'three');

Translated, this is what is happening here:

$_GET['foobar'][] = 'one';
$_GET['foobar'][] = 'two';
$_GET['foobar'][] = 'three';

(Note: $array[] = 'value' is the same as array_push($array, 'value'))

So whenever you're dealing with repeating key names (or <select multiple>) you want to add [] to the name, so PHP builds an array from it.

You may also want to know that you can actually specify the array-keys:

<input type="text" name="foobar[hello][world]" value="one">

will lead to $_GET['foobar']['hello']['world'] == 'one'.

Comments

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.