来自数组第二维的 $_POST 值

发布于 2024-09-08 02:30:23 字数 949 浏览 1 评论 0原文

使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

去了角落 2024-09-15 02:30:23

你快到了。这应该有效:

foreach($_POST as $k=>$v) {

     if (is_array($v) == true) {

        foreach($v as $value) {

           echo htmlspecialchars($value); // Always sanitize when you output! :) 
           echo "<br>";

        }

    }

}

You're almost there. This should work:

foreach($_POST as $k=>$v) {

     if (is_array($v) == true) {

        foreach($v as $value) {

           echo htmlspecialchars($value); // Always sanitize when you output! :) 
           echo "<br>";

        }

    }

}
倒数 2024-09-15 02:30:23

上面的代码不起作用的简单原因是因为您正在检查数组 key 是否是一个数组,而不是实际值本身。交换...

if (is_array($k) == true)

if (is_array($v) == true)

在嵌套的 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...

if (is_array($k) == true)

with

if (is_array($v) == true)

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.

二智少女 2024-09-15 02:30:23
foreach( $_POST as $key => $item ) {
    if ( is_array($item) ) { // you want to check if the value is an array, not the key
        foreach($item as $index => $value) {
            echo $value . "\n<br>";
        }
    }
}
foreach( $_POST as $key => $item ) {
    if ( is_array($item) ) { // you want to check if the value is an array, not the key
        foreach($item as $index => $value) {
            echo $value . "\n<br>";
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文