PHP 指定数组值?传递给 each() 的变量不是数组或对象


php specifying array values? Variable passed to each() is not an array or object

初学者PHP程序员在这里尝试使用mailchimp API...

关于我在这里的第一行代码,我不断收到这个 PHP 错误...... 如您所见,我正在从表单中发布一个值,但我不确定发生了什么。

错误:警告:传递给 each() 的变量不是第 24 行 xxxx 中的数组或对象(这是第一行)

while (list($key, $val) = each($_POST['MCgroup'])) {
        $interest .= ($count>0 ? ", " : "");
        $interest .= $val;
        $count++;
    }
    echo $interest; //echos NOTHINGblank
    $mergeVars = array(
                       'INTERESTS'=>$interest
                        );
    echo $mergeVars; //echos the word ARRAY

根据我的研究,显然我没有传递实际的数组......但从我的发现来看,我的语法似乎都是正确的。(但我可能忽略了一些东西)。

这是传递值的表单代码部分

 <input type="hidden" name="MCgroup" id="MCgroup" value="My Interest One" />
 <input type="hidden" name="MCgroup" id="MCgroup" value="My Interest Two" />

或者我可以将其更改为

<input type="hidden" name="MCgroup" id="MCgroup" value="My Interest One, My Interest Two" />

我只是不确定如何将其制作成一个数组。

请帮忙!谢谢!

您可以通过后

[]<input>设置为具有数组名称,执行以下操作:

<input type="hidden" name="MCgroup[]" id="MCgroup" value="My Interest One" /> <input type="hidden" name="MCgroup[]" id="MCgroup" value="My Interest Two" />

然后通过以下方式进行检查:

print_r($_POST['MCgroup']);

我假设您将表单方法设置为 POST。

将 HTML 名称更改为数组:

 <input type="hidden" name="MCgroup[]" id="MCgroup1" value="My Interest One" />
 <input type="hidden" name="MCgroup[]" id="MCgroup2" value="My Interest Two" />
 <input type="hidden" name="MCgroup[]" id="MCgroup3" value="And another sparkling interest" />

也只在id是唯一的时才使用id。