简体   繁体   中英

What's wrong with this php foreach code?

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

$_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.

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' .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM