Php 使用foreach/for循环从多维数组中访问要在HTML表中显示的值

Php 使用foreach/for循环从多维数组中访问要在HTML表中显示的值,php,arrays,yii2,Php,Arrays,Yii2,我试图在表上显示每组数组(第一维)。在每次页面加载中都会创建大量的数组和表,这就是我想到使用foreach的原因。返回的数组来自控制器 我返回的数组如下所示: 数组:$appreq Array ( [0] => Array ( [0] => Array ( [Name] => Lot Survey Plans and Specifications

我试图在表上显示每组数组(第一维)。在每次页面加载中都会创建大量的数组和表,这就是我想到使用foreach的原因。返回的数组来自控制器

我返回的数组如下所示:

数组:$appreq

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [Name] => Lot Survey Plans and Specifications
                    [Document] => -
                    [Id] => 10
                    [Is_Received] => 0
                    [Is_Personal_Submission] => 1
                )

            [1] => Array
                (
                    [Name] => Others (Specify)
                    [Document] => -
                    [Id] => 11
                    [Is_Received] => 0
                    [Is_Personal_Submission] => 1
                )

        )

    [1] => Array
        (
            [0] => Array
                (
                    [Name] => Fire Protection Plan (if applicable)
                    [Document] => 
                    [Id] => 12
                    [Is_Received] => 0
                    [Is_Personal_Submission] => 1
                )

        )

    [2] => Array
        (
            [0] => Array
                (
                    [Name] => Vicinity Map or Location Plan within a Two(2) Kilometer Radius
                    [Document] => -
                    [Id] => 13
                    [Is_Received] => 0
                    [Is_Personal_Submission] => 1
                )

            [1] => Array
                (
                    [Name] => Site Development Plan
                    [Document] => assets/uploads/ca060d9b-93d2-4fcd-8a8b-113bfac8dbbf/Architectural Permit/Site Development Plan...hollow-knight.jpg
                    [Id] => 14
                    [Is_Received] => 0
                    [Is_Personal_Submission] => 1
                )

            [2] => Array
                (
                    [Name] => Perspective
                    [Document] => -
                    [Id] => 15
                    [Is_Received] => 0
                    [Is_Personal_Submission] => 1
                )
数组:$辅助许可证

该表应显示在第一个D数组中分割的数据,然后行是数组的下一个维度,my td应包含:Name和is_Received值。请不要介意表格标题(本地许可证等),它是另一个foreach循环的一部分,我只是有麻烦与tbody

从数组:$appreq

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [Name] => Lot Survey Plans and Specifications
                    [Document] => -
                    [Id] => 10
                    [Is_Received] => 0
                    [Is_Personal_Submission] => 1
                )

            [1] => Array
                (
                    [Name] => Others (Specify)
                    [Document] => -
                    [Id] => 11
                    [Is_Received] => 0
                    [Is_Personal_Submission] => 1
                )

        )

    [1] => Array
        (
            [0] => Array
                (
                    [Name] => Fire Protection Plan (if applicable)
                    [Document] => 
                    [Id] => 12
                    [Is_Received] => 0
                    [Is_Personal_Submission] => 1
                )

        )

    [2] => Array
        (
            [0] => Array
                (
                    [Name] => Vicinity Map or Location Plan within a Two(2) Kilometer Radius
                    [Document] => -
                    [Id] => 13
                    [Is_Received] => 0
                    [Is_Personal_Submission] => 1
                )

            [1] => Array
                (
                    [Name] => Site Development Plan
                    [Document] => assets/uploads/ca060d9b-93d2-4fcd-8a8b-113bfac8dbbf/Architectural Permit/Site Development Plan...hollow-knight.jpg
                    [Id] => 14
                    [Is_Received] => 0
                    [Is_Personal_Submission] => 1
                )

            [2] => Array
                (
                    [Name] => Perspective
                    [Document] => -
                    [Id] => 15
                    [Is_Received] => 0
                    [Is_Personal_Submission] => 1
                )
第一个维度:[0]=>数组是本地清除表

=>数组是消防安全表

=>数组是架构许可表

预期表格:

我使用foreach变量的方式以及调用数组以在中显示的方式一定有问题。我真的希望有人能帮我度过难关,提前谢谢

更新--------------

我按照建议尝试了以下操作:

<?php foreach($ancillarypermit as $row => $data) : 
     foreach($appreq as $row => $value) : ?>

    <table style="text-align: left; width: 50%">
        <thead>
            <tr>
            <th scope="col">#</th>
            <th width='10' style="text-align:center;">Received</th>
            <th scope="col">Requirement</th>
            </tr>
        </thead>
        <tbody>
        <h5><strong><?= $data->permit->Name ?></strong></h5>
        <?php 
            foreach ($value as $key => $values) :
            ?>
                <tr>
                    <th scope="row">1</th>
                    <td width='150' style='text-align: center; vertical-align: middle; padding: 3px'>
                        <?php 
                        echo CheckboxX::widget([
                            'name'=>'s_1', 
                            'value'=>$values['Is_Received'], 
                            // 'readonly'=>true, 
                            'pluginOptions'=>['threeState'=>false]
                        ]);
                        ?>
                    </td>
                    <td> <?= $values['Name']  ?> </td>
                </tr>
            <?php endforeach; ?>
        </tbody>
    </table>
<?php endforeach; 
endforeach;?>

#
收到
要求

1.
但我得到的结果是:

您要做的是,将数组循环两次,从而进入两次。 在第一次迭代中,
$data

Array(
    [0] => Array(
        [Name] => Lot Survey Plans and Specifications
        [Document] => -
        [Id] => 10
        [Is_Received] => 0
        [Is_Personal_Submission] => 1
    )
    [1] => Array(
        [Name] => Others (Specify)
        [Document] => -
        [Id] => 11
        [Is_Received] => 0
        [Is_Personal_Submission] => 1
    )
)
Array(
    [Name] => Lot Survey Plans and Specifications
    [Document] => -
    [Id] => 10
    [Is_Received] => 0
    [Is_Personal_Submission] => 1
)
在第二个foreach的第一次迭代中,
$values

Array(
    [0] => Array(
        [Name] => Lot Survey Plans and Specifications
        [Document] => -
        [Id] => 10
        [Is_Received] => 0
        [Is_Personal_Submission] => 1
    )
    [1] => Array(
        [Name] => Others (Specify)
        [Document] => -
        [Id] => 11
        [Is_Received] => 0
        [Is_Personal_Submission] => 1
    )
)
Array(
    [Name] => Lot Survey Plans and Specifications
    [Document] => -
    [Id] => 10
    [Is_Received] => 0
    [Is_Personal_Submission] => 1
)
更新:

您的代码应该类似于

<?php 
    foreach($appreq as $row => $data): 
    // table name print
    // table headers print
        foreach ($data as $key => $values) :
            //print row content by calling $values['Is_Received'], $values['Name'], ...
        endforeach;
    endforeach
?>

我希望这能回答你的问题。 如果还有什么不清楚的,请告诉我

更新2

<?php 
    foreach($appreq as $row => $value) : ?>

    <table style="text-align: left; width: 50%">
        <thead>
            <tr>
            <th scope="col">#</th>
            <th width='10' style="text-align:center;">Received</th>
            <th scope="col">Requirement</th>
            </tr>
        </thead>
        <tbody>
        <h5><strong><?= $ancillarypermit[$row]->permit->Name ?></strong></h5>
        <?php 
            foreach ($value as $key => $values) :
            ?>
                <tr>
                    <th scope="row">1</th>
                    <td width='150' style='text-align: center; vertical-align: middle; padding: 3px'>
                        <?php 
                        echo CheckboxX::widget([
                            'name'=>'s_1', 
                            'value'=>$values['Is_Received'], 
                            // 'readonly'=>true, 
                            'pluginOptions'=>['threeState'=>false]
                        ]);
                        ?>
                    </td>
                    <td> <?= $values['Name']  ?> </td>
                </tr>
            <?php endforeach; ?>
        </tbody>
    </table>
<?php endforeach;?>

#
收到
要求

1.

您要做的是,将数组循环两次,从而进入两次。 在第一次迭代中,
$data

Array(
    [0] => Array(
        [Name] => Lot Survey Plans and Specifications
        [Document] => -
        [Id] => 10
        [Is_Received] => 0
        [Is_Personal_Submission] => 1
    )
    [1] => Array(
        [Name] => Others (Specify)
        [Document] => -
        [Id] => 11
        [Is_Received] => 0
        [Is_Personal_Submission] => 1
    )
)
Array(
    [Name] => Lot Survey Plans and Specifications
    [Document] => -
    [Id] => 10
    [Is_Received] => 0
    [Is_Personal_Submission] => 1
)
在第二个foreach的第一次迭代中,
$values

Array(
    [0] => Array(
        [Name] => Lot Survey Plans and Specifications
        [Document] => -
        [Id] => 10
        [Is_Received] => 0
        [Is_Personal_Submission] => 1
    )
    [1] => Array(
        [Name] => Others (Specify)
        [Document] => -
        [Id] => 11
        [Is_Received] => 0
        [Is_Personal_Submission] => 1
    )
)
Array(
    [Name] => Lot Survey Plans and Specifications
    [Document] => -
    [Id] => 10
    [Is_Received] => 0
    [Is_Personal_Submission] => 1
)
更新:

您的代码应该类似于

<?php 
    foreach($appreq as $row => $data): 
    // table name print
    // table headers print
        foreach ($data as $key => $values) :
            //print row content by calling $values['Is_Received'], $values['Name'], ...
        endforeach;
    endforeach
?>

我希望这能回答你的问题。 如果还有什么不清楚的,请告诉我

更新2

<?php 
    foreach($appreq as $row => $value) : ?>

    <table style="text-align: left; width: 50%">
        <thead>
            <tr>
            <th scope="col">#</th>
            <th width='10' style="text-align:center;">Received</th>
            <th scope="col">Requirement</th>
            </tr>
        </thead>
        <tbody>
        <h5><strong><?= $ancillarypermit[$row]->permit->Name ?></strong></h5>
        <?php 
            foreach ($value as $key => $values) :
            ?>
                <tr>
                    <th scope="row">1</th>
                    <td width='150' style='text-align: center; vertical-align: middle; padding: 3px'>
                        <?php 
                        echo CheckboxX::widget([
                            'name'=>'s_1', 
                            'value'=>$values['Is_Received'], 
                            // 'readonly'=>true, 
                            'pluginOptions'=>['threeState'=>false]
                        ]);
                        ?>
                    </td>
                    <td> <?= $values['Name']  ?> </td>
                </tr>
            <?php endforeach; ?>
        </tbody>
    </table>
<?php endforeach;?>

#
收到
要求

1.

能否请您添加要显示的预期html表格。@Inaseskull Hi,我更新了我的问题并添加了图像,谢谢您请添加要显示的预期html表格。@Inaseskull Hi,我更新了我的问题并添加了图像,谢谢您更快地发布了此答案,我删除了我的答案:)@wolfQueen我希望这能回答您的问题。如果还有别的事,请告诉我。你可以看到下面UPDATE@wolfQueen. 我现在无法为您编写代码,但请不要在上面的示例中,您需要在的2个
之间放置一些东西。将新的表格标题放在那里。@wolfQueen您能给我看一下
$ancillarypermi
数组吗?如果您可以编辑问题以显示表数组,我可以告诉您如何处理它。@wolfQueen我已经编辑了我的答案。我不太确定它是否能工作,因为我不能100%确定结构看起来如何。如果没有,我建议切换到聊天,这样我们就不会在这里发送垃圾邮件,我会调查并帮助您,稍后用正确的最终信息编辑答案。您更快地发布了此答案,我删除了我的答案:)@wolfQueen我希望这能回答您的问题。如果还有别的事,请告诉我。你可以看到下面UPDATE@wolfQueen. 我现在无法为您编写代码,但请不要在上面的示例中,您需要在
的2个
之间放置一些东西。将新的表格标题放在那里。@wolfQueen您能给我看一下
$ancillarypermi
数组吗?如果您可以编辑问题以显示表数组,我可以告诉您如何处理它。@wolfQueen我已经编辑了我的答案。我不太确定它是否能工作,因为我不能100%确定结构看起来如何。如果没有,我建议切换到聊天,这样我们就不会在这里发垃圾邮件,我会调查它,帮助你,然后用正确的最终信息编辑答案。