Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
这个php foreach代码有什么问题?_Php - Fatal编程技术网

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

这个php foreach代码有什么问题?,php,Php,提交后,它将回显$value。我有一个if语句检查submit按钮是否设置为yada-yada,在我按下submit按钮后,它返回以下错误->警告:在第43行的C:\xampp\htdocs\yada\yada-yada.php中为foreach提供的参数无效$\u POST['menuItems']不是数组,foreach只接受数组和某些对象 如果你成功了 <?php $sessionTotal = 10; for($initial = 1; $initial <

提交后,它将回显$value。我有一个if语句检查submit按钮是否设置为yada-yada,在我按下submit按钮后,它返回以下错误->警告:在第43行的C:\xampp\htdocs\yada\yada-yada.php中为foreach提供的参数无效

$\u POST['menuItems']不是数组,foreach只接受数组和某些对象

如果你成功了

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

?>
它应该可以工作。

$\u POST['menuItems']不是数组,foreach只接受数组和某些对象

如果你成功了

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

?>

它应该能用。

你的foreach没有问题。您对PHP如何解析输入属性的理解有问题

因此,基本上它是将$\u GET['foobar']赋值三次,留下$\u GET['foobar']='三次'

翻译过来,这里发生了这样的事情:

<?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]);
}
<input type="text" name="foobar[]" value="one">
<input type="text" name="foobar[]" value="two">
<input type="text" name="foobar[]" value="three">
在这一点上,我想指出,其他语言Ruby、Java……对此的处理方式完全不同。例如,Ruby识别重复键并构建类似于$_GET['foobar']=array'one'、'two'、'three'的东西

有一个简单的技巧可以告诉PHP应该将重复值解析为数组:

$_GET['foobar'] = 'one';
$_GET['foobar'] = 'two';
$_GET['foobar'] = 'three';
注意:$array[]=“value”与array\u push$array“value”相同

因此,每当您处理重复的键名或希望向名称中添加[]时,PHP都会从中构建一个数组

您可能还想知道,您实际上可以指定数组键:

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

将导致$_GET['foobar']['hello']['world']=='one'。

您的foreach没有问题。您对PHP如何解析输入属性的理解有问题

因此,基本上它是将$\u GET['foobar']赋值三次,留下$\u GET['foobar']='三次'

翻译过来,这里发生了这样的事情:

<?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]);
}
<input type="text" name="foobar[]" value="one">
<input type="text" name="foobar[]" value="two">
<input type="text" name="foobar[]" value="three">
在这一点上,我想指出,其他语言Ruby、Java……对此的处理方式完全不同。例如,Ruby识别重复键并构建类似于$_GET['foobar']=array'one'、'two'、'three'的东西

有一个简单的技巧可以告诉PHP应该将重复值解析为数组:

$_GET['foobar'] = 'one';
$_GET['foobar'] = 'two';
$_GET['foobar'] = 'three';
注意:$array[]=“value”与array\u push$array“value”相同

因此,每当您处理重复的键名或希望向名称中添加[]时,PHP都会从中构建一个数组

您可能还想知道,您实际上可以指定数组键:

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

将导致$_GET['foobar']['hello']['world']=='one'。

您的帖子没有菜单项!发布print\u r$\u post的输出;或者$\u POST['menuItems']不是数组。您的帖子没有menuItems条目!发布print\u r$\u post的输出;或者$\u POST['menuItems']不是数组..酷,谢谢,伙计。它现在可以工作了,为什么[]将其设置为数组?我会在8分钟内接受你的回答,这不允许我在8分钟前接受-1因为我没有解释为什么需要这些[]。根据您的回答,OP可能有一个有效的示例,但仍然没有关于为什么会发生这种情况的第一条线索。@MouseHello,因为数组就是这样指定的:变量[key],如果您不输入一个a键,它会在数组中创建一个新值。很酷,谢谢,伙计。它现在可以工作了,为什么[]将其设置为数组?我会在8分钟内接受你的回答,这不允许我在8分钟前接受-1因为我没有解释为什么需要这些[]。根据您的回答,OP可能有一个有效的示例,但仍然没有关于为什么会发生这种情况的第一条线索。@MouseHello,因为数组是这样指定的:variable[key],如果您不放置一个a键,它会在数组中创建一个新值。