简体   繁体   English

这个php foreach代码有什么问题?

[英]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. 提交后,它将回显$ value。 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 我有一个if语句,检查按提交按钮后是否设置了提交按钮yada yada,它返回此错误->警告:为C:\\ xampp \\ htdocs \\ yada \\ yada-yada中的foreach()提供了无效的参数.php行43

$_POST['menuItems'] is not an array, foreach only accepts arrays and certain objects. $_POST['menuItems']不是数组, foreach仅接受数组和某些对象。

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 . 没有什么不对您foreach There is something wrong with your understanding of how PHP parses input-attributes (_POST, _GET). 您对PHP如何解析输入属性(_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 . 转换为application / x-www-form-urlencoded表示形式 foobar=one&foobar=two&foobar=three

PHP parses this string into a map (associative array). PHP将此字符串解析为一个映射(关联数组)。 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' . 所以基本上它是分配$_GET['foobar']三次,剩下$_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,Java等)的处理方式大不相同。 Ruby for example recognizes the repeating key and builds something similar to $_GET['foobar'] = array('one', 'two', 'three') . 例如,Ruby识别重复键并构建类似于$_GET['foobar'] = array('one', 'two', 'three')


There is a simple "trick" to tell PHP that the repeating value should be parsed into an array: 有一个简单的“技巧”告诉PHP应该将重复值解析为数组:

<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') ; 将导致$_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') ) (注意: $array[] = 'value'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. 因此,无论何时要处理重复的键名(或<select multiple> ),都想在名称中添加[] ,因此PHP会从中构建一个数组。

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' . 将导致$_GET['foobar']['hello']['world'] == 'one'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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