0

我有 $_POST 数组

    Array
(
    [dddd] => Array
        (
            [students] => Array
                (
                    [парарпа] => Array
                        (
                            [zach1] => 1
                            [zach2] => 1
                            [zach3] => 2
                            [zach4] => 3                               
                            [ekz1] => 5
                            [ekz2] => 5
                            [ekz3] => 5
                            [ekz4] =>
                        )

                    [ававп] => Array
                        (
                            [zach1] => 3
                            [zach2] => 5
                            [zach3] => 4
                            [zach4] => 2
                            [ekz1] => 4
                            [ekz2] => 3
                            [ekz3] => 2
                            [ekz4] => 
                        )

                )

            [zach1] => рпа
            [zach2] => рпарпарпар
            [zach3] => 
            [zach4] => пар
            [ekz1] => пав
            [ekz2] => авпав
            [ekz3] => ва
            [ekz4] => 
        )

    [ggg] => Array
        (
            [students] => Array
                (
                    [авпва] => Array
                        (
                            [zach1] => 3
                            [zach2] => 3
                            [zach3] => 2
                            [zach4] => 
                            [ekz1] => 4
                            [ekz2] => 
                            [ekz3] => 
                            [ekz4] => 
                        )

                )

            [zach1] => паыв
            [zach2] => авы
            [zach3] => ыва
            [zach4] => 
            [ekz1] => выа
            [ekz2] => 
            [ekz3] => 
            [ekz4] => 
        )

    [fffff] => Array
        (
            [students] => Array
                (
                    [авыв] => Array
                        (
                            [zach1] => 5
                            [zach2] => 5
                            [zach3] => 
                            [zach4] => 
                            [ekz1] => 4
                            [ekz2] => 4
                            [ekz3] => 
                            [ekz4] => 
                        )

                    [ваыыва] => Array
                        (
                            [zach1] => 3
                            [zach2] => 3
                            [zach3] => 
                            [zach4] => 
                            [ekz1] => 2
                            [ekz2] => 3
                            [ekz3] => 
                            [ekz4] => 
                        )

                )

            [zach1] => ва
            [zach2] => ва
            [zach3] => 
            [zach4] => 
            [ekz1] => ва
            [ekz2] => ва
            [ekz3] => 
            [ekz4] => 
        )

)

我正在努力

foreach($_POST as $groupName=>$data) {echo $groupName;}

它只呼应最后一个“fffff”。我不知道到底发生了什么。请帮帮我。回声计数($_POST);回声 3

4

2 回答 2

0

尝试这个:

<?php

// Dummy Post Data
$_POST = array(
    'dddd' => array(
        'students' => array(
            'парарпа' => array('zach1' => 1, 'zach2' => 2, 'zach3' => 2),
            'ававп' => array('zach1' => 3, 'zach2' => 5, 'zach3' => 4),
        ),
        'zach1' => 'рпа',
        'zach2' => 'рпарпарпар'
    ),
    'ggg' => array(
        'students' => array(
            'парарпа' => array('zach1' => 1, 'zach2' => 2, 'zach3' => 2),
            'ававп' => array('zach1' => 3, 'zach2' => 5, 'zach3' => 4),
        ),
        'zach1' => 'рпа',
        'zach2' => 'рпарпарпар'
    ),
    'fffff' => array(
        'students' => array(
            'парарпа' => array('zach1' => 1, 'zach2' => 2, 'zach3' => 2),
            'ававп' => array('zach1' => 3, 'zach2' => 5, 'zach3' => 4),
        ),
        'zach1' => 'рпа',
        'zach2' => 'рпарпарпар'
    ),
);

// Test Iteration
foreach ($_POST as $key => $data)
{
    echo "<b>$key</b><br />";
    foreach ($data as $innerKey => $innerData)
    {
        if (is_array($innerData))
        {
            echo "<i>$innerKey</i><br />";
            foreach ($innerData as $studentName => $studentData)
            {
                echo "Student [$studentName] = ";
                foreach ($studentData as $studentDataKey => $studentDataValue) {
                    echo "$studentDataKey => $studentDataValue | ";
                }
            }
        }
        else {
            echo "<i>$innerKey</i> = $innerData<br />";
        }
    }
    echo "<hr />";
}

?>

输出:

在此处输入图像描述

我可以很好地遍历它。

于 2013-10-25T09:30:17.143 回答
0

解决了

$keys = array_keys($_POST);

for($i = 0; $i < count($keys); $i++)
{
    $data = $_POST[$keys[$i]];
    $groupName = $keys[$i];
}

仍然不知道为什么 foreach 会那样工作

于 2013-10-25T12:43:58.147 回答