通过POST发送多维数组

时间:2016-04-05 12:31:07

标签: php post multidimensional-array

我使用以下命令通过POST发送多维数组:

<form action="fixtures.php" method="POST">

    <input type="hidden" name="day" value="<?= $day ?>">
    <input type="hidden" name="month" value="<?= $month ?>">
    <input type="hidden" name="year" value="<?= $year ?>">
    <input type="hidden" name="league" value="<?= $league ?>">
    <input type="hidden" name="addFixtures" value="1">

    <?php

    for($i = 0; $i < count($fixtures); $i++){

         $team_a_name = $fixtures[$i]['team-a-name'];
         $team_b_name = $fixtures[$i]['team-b-name'];
         $fixtDate = $fixtures[$i]['Date'];

         echo "<input type='hidden' name=\"fixtures[$i]['team-a-name']\" value='$team_a_name'>";
         echo "<input type='hidden' name=\"fixtures[$i]['team-b-name']\" value='$team_b_name'>";
         echo "<input type='hidden' name=\"fixtures[$i]['date']\" value='$fixtDate'>";

    }

    ?>

    <input type="submit" value="Add Fixtures">

</form>

但是,当我尝试使用以下内容访问$_POST['fixtures']中存储的值时:

if(isset($_POST['addFixtures'])){

    print_r($_POST['fixtures'][0]);
    echo "<br><br>";
    print($_POST['fixtures'][0]['date']);

    exit;

}

我得到了这个输出:

  

数组([&#39; team-a-name&#39;] =&gt;团队A [&#39; team-b-name&#39;] =&gt;团队B [&#39; date&# 39;] =&gt; 5-4-2016)

     

注意:未定义的索引:日期   /Applications/XAMPP/xamppfiles/htdocs/soccerdome/fixtures.php上线   33

如果我尝试访问&#39; team-a-name&#39;或者&#39; team-b-name&#39;。 当print_r $_POST['fixtures'][0]清楚地显示他们在数组中时,为什么我无法访问这些值呢?

修改

打印出$_POST[]时,我得到:

    Array
(
    [day] => 5
    [month] => 4
    [year] => 2016
    [league] => prem
    [addFixtures] => 1
    [fixtures] => Array
        (
            [0] => Array
                (
                    ['team-a-name'] => TEAM A
                    ['team-b-name'] => TEAM B
                    ['date'] => 5-4-2016
                )

            [1] => Array
                (
                    ['team-a-name'] => TEAM C
                    ['team-b-name'] => TEAM D
                    ['date'] => 5-4-2016
                )

            [2] => Array
                (
                    ['team-a-name'] => TEAM A
                    ['team-b-name'] => TEAM D
                    ['date'] => 12-4-2016
                )

            [3] => Array
                (
                    ['team-a-name'] => TEAM B
                    ['team-b-name'] => TEAM C
                    ['date'] => 12-4-2016
                )

            [4] => Array
                (
                    ['team-a-name'] => TEAM A
                    ['team-b-name'] => TEAM C
                    ['date'] => 19-4-2016
                )

            [5] => Array
                (
                    ['team-a-name'] => TEAM D
                    ['team-b-name'] => TEAM B
                    ['date'] => 19-4-2016
                )

        )

)

编辑2

由于某种原因它现在正在工作!我不知道如何,只是多次刷新页面,错误消息消失,POST内的值被打印!

3 个答案:

答案 0 :(得分:2)

遍历帖子数据的最简单方法是使用foreach。你可以这样做:

if(isset($_POST['addFixtures']))
{
    foreach($_POST['fixtures'] as $key=>$value)
    {
      echo "<br><br>";
      print($value['date']);
    }
}
exit;

答案 1 :(得分:0)

由于date中没有任何特殊字符,最好不要使用'

 echo "<input type='hidden' name=\"fixtures[$i]['team-a-name']\" value='$team_a_name'>";
 echo "<input type='hidden' name=\"fixtures[$i]['team-b-name']\" value='$team_b_name'>";
 echo "<input type='hidden' name=\"fixtures[$i][date]\" value='$fixtDate'>";

或将其更改为:

 echo "<input type='hidden' name=\"fixtures[$i][team-a-name]\" value='$team_a_name'>";
 echo "<input type='hidden' name=\"fixtures[$i][team-b-name]\" value='$team_b_name'>";
 echo "<input type='hidden' name=\"fixtures[$i][date]\" value='$fixtDate'>";

始终使用var_dump($_POST)print_r($_POST)来检查现实中发生的事情。 :)

答案 2 :(得分:0)

print_r整个post array.i.e.print_r($ _ POST)。然后你将分配确切的键和值,并成功显示数组。