来自数组第二维的 $_POST 值
使用PHP。我试图从二维 $_POST 数组的第二个维度检索 $_POST 值,但实际上并不知道所发布的值的名称。这是我所拥有的;它不起作用。
foreach($_POST as $k=>$v) {
$$k=$v;
if (is_array($k) == true) {
foreach($k as $value) {
echo $value;
echo "<br>";
}
}
}
我曾经
echo '<pre>';
print_r($_POST);
echo '</pre>';
确保数组中有值并且确实存在。它显示:
Array
(
[colors] => Array
(
[0] => red
[1] => yellow
[2] => blue
[3] => black
)
)
这就是我的表单复选框的样子:
<input name="colors[]" type="checkbox" value="red" />
<input name="colors[]" type="checkbox" value="yellow" />
<input name="colors[]" type="checkbox" value="blue" />
<input name="colors[]" type="checkbox" value="black" />
如果我不知道数组的名称,如何从数组中的数组中获取值。名字不总是颜色吗?
Using PHP. I'm trying to retrieve $_POST values from a the second dimension of a two dimensional $_POST array without actually knowing the names of the values being posted. Here is what I have; it doesn't work.
foreach($_POST as $k=>$v) {
$k=$v;
if (is_array($k) == true) {
foreach($k as $value) {
echo $value;
echo "<br>";
}
}
}
I used
echo '<pre>';
print_r($_POST);
echo '</pre>';
to make sure there are values in the arrays and there are. It shows:
Array
(
[colors] => Array
(
[0] => red
[1] => yellow
[2] => blue
[3] => black
)
)
This is what my form check boxes look like:
<input name="colors[]" type="checkbox" value="red" />
<input name="colors[]" type="checkbox" value="yellow" />
<input name="colors[]" type="checkbox" value="blue" />
<input name="colors[]" type="checkbox" value="black" />
How do I get the values from an array within an array if I don't know the name of the array. The name won't always be colors?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你快到了。这应该有效:
You're almost there. This should work:
上面的代码不起作用的简单原因是因为您正在检查数组
key
是否是一个数组,而不是实际值本身。交换...与
在嵌套的 foreach 子句中执行相同的操作。
它应该工作得很好。另外,您也可以删除布尔比较,is_array 返回一个布尔值,您并没有使其比现在更明确。
The simple reason why your above code does not work is because you're checking whether the array
key
is an array, not the actual value itself. Swap out...with
Do the same in the nested foreach clause.
And it should work fine. Also, you might as well drop the boolean comparison, is_array returns a boolean, you're not making it any more explicit than it already is.