使用_POST在php中检索嵌套数组

时间:2013-04-11 23:17:16

标签: php arrays

我在HTML表单中设置了以下内容。如何在我的PHP页面中找回它(ON提交)?

foreach ($rows as $i => $value) {
    echo "<br/>What do you think about : ";
    echo "<li>$value<br/>";
    echo "1<input type='radio' value='1' name='answer-$i' />&nbsp;";
    echo "2<input type='radio' value='2' name='answer-$i' />&nbsp;";
    echo "3<input type='radio' value='3' name='answer-$i' />&nbsp;";
    echo "4<input type='radio' value='4' name='answer-$i' />&nbsp;";
    echo "5<input type='radio' value='5' name='answer-$i' />&nbsp;";    
    }
?>

如何找回我的答案?

我为我的表单使用POST方法。帮帮我!!

添加详细信息。

<?php
echo "hello";
$answer = _POST("answer");
echo $answer-0;
echo $answer[0];
foreach ($answer as $i => $value) {

    echo $value;
    echo $i;
}

?>

回答-0 = 3&amp; answer-1 = 1

Above Values显示参数已设置(Get Method)。但在POST METHOD中我无法访问它们。

有人可以帮帮我吗?我还没有解决这个问题。

2 个答案:

答案 0 :(得分:0)

所以你需要使用带有方括号的$_POST数组(它是一个数组)。假设您仍有$answers可用,此代码将为您构建一个$rows数组。

$answers = array();
foreach ($rows as $i => $value) {
    $answers[$i] = $_POST["answer-$i"];
}
print_r($answers);

答案 1 :(得分:0)

<form method="post">
<?php

foreach ($_POST as $i => $value) {
    echo "$i&nbsp;$value<br/>";
}

echo $_POST['answer-1'];
echo "<br>";
echo $_POST['answer-2'];

$rows = array(
    "1" => "bar",
    "2" => "foo",
);

foreach ($rows as $i => $value) {
    echo "<br/>What do you think about : ";
    echo "<li>$value<br/>";
    echo "1<input type='radio' value='1' name='answer-$i' />&nbsp;";
    echo "2<input type='radio' value='2' name='answer-$i' />&nbsp;";
    echo "3<input type='radio' value='3' name='answer-$i' />&nbsp;";
    echo "4<input type='radio' value='4' name='answer-$i' />&nbsp;";
    echo "5<input type='radio' value='5' name='answer-$i' />&nbsp;";    
    }
?>
<input type="submit"/>
</form>

看起来更像是,你需要什么。