在视图页面PHP中显示数组数组

时间:2018-04-27 16:10:06

标签: php mysql codeigniter

我从控制器获取数组数组的结果。 这里有一个是主数组,“bom_dt”下是数组数组。 我知道如何显示主数组,但不确定主数组下的数组。我被困在这里。请给我一些建议,

 Array
    (
        [0] => Array
            (
                [tih_sku] => 10001
                [tih_style] => RHH/110/02
                [tih_color] => BLACK
                [tih_leather] => PERU
                [bom_dt] => Array
                    (
                        [0] => Array
                            (
                                [mm_spec_fld] => *-*-*-*-*-NO 5, 22.5 CM-*-*-*-*-*-*-*-*-*-*-*-METAL-*-*-*-*-*-*-*-*-*-*-*-*-BLACK-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
                                [tbid_qnty] => 2
                                [mi_sku] => RHH/110/02 BLACK
                            )

                        [1] => Array
                            (
                                [mm_spec_fld] => *-*-*-*-*-NO 3-*-*-*-*-*-*-*-*-*-*-*-NYLON-*-*-*-*-*-*-*-*-*-*-*-*--*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
                                [tbid_qnty] => 8.75
                                [mi_sku] => RHH/110/02 BLACK
                            )

                        [2] => Array
                            (
                                [mm_spec_fld] => *-*-*-*-*-*-BLG-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-MD1-NA-NA-NA-NA-NA-NA-NA-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
                                [tbid_qnty] => 1
                                [mi_sku] => RHH/110/02 BLACK
                            )

                        [3] => Array
                            (
                                [mm_spec_fld] => *-*-*-*-*-*-BLG-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-CCF2-NA-NA-NA-NA-NA-NA-NA-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
                                [tbid_qnty] => 1
                                [mi_sku] => RHH/110/02 BLACK
                            )

                        [4] => Array
                            (
                                [mm_spec_fld] => *-*-GROSSGRAIN-GRANITE-*-*-*-*-*-*-*-*-*-----*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
                                [tbid_qnty] => 0.9
                                [mi_sku] => RHH/110/02 BLACK
                            )


                    )

            )

        [1] => Array
            (
                [tih_sku] => 10009
                [tih_style] => RHH/110/04
                [tih_color] => BLACK
                [tih_leather] => PERU
                [bom_dt] => Array
                    (
                        [0] => Array
                            (
                                [mm_spec_fld] => *-*-*-*-*-NO 5, 44.5 CM-*-*-*-*-*-*-*-*-*-*-*-METAL-*-*-*-*-*-*-*-*-*-*-*-*-BLACK-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
                                [tbid_qnty] => 1
                                [mi_sku] => RHH/110/04 BLACK
                            )

                        [1] => Array
                            (
                                [mm_spec_fld] => *-*-*-*-*-NO 3-*-*-*-*-*-*-*-*-*-*-*-NYLON-*-*-*-*-*-*-*-*-*-*-*-*--*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
                                [tbid_qnty] => 7.75
                                [mi_sku] => RHH/110/04 BLACK
                            )

                        [2] => Array
                            (
                                [mm_spec_fld] => *-*-*-*-*-*-BLG-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-FRAME-RECTANGULAR-16MM-NA-NA-NA-NA-PIECES-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
                                [tbid_qnty] => 2
                                [mi_sku] => RHH/110/04 BLACK
                            )

                        [3] => Array
                            (
                                [mm_spec_fld] => *-*-*-*-*-*-BLG-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-R.S.FITTINGS-NA-NA-NA-NA-NA-NA-PIECES-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
                                [tbid_qnty] => 2
                                [mi_sku] => RHH/110/04 BLACK
                            )

                        [4] => Array
                            (
                                [mm_spec_fld] => *-*-*-*-*-*-BLG-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-MD1-NA-NA-NA-NA-NA-NA-PIECES-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
                                [tbid_qnty] => 1
                                [mi_sku] => RHH/110/04 BLACK
                            )

                    )

            )

        [2] => Array
            (
                [tih_sku] => 10230
                [tih_style] => RHN/208/02
                [tih_color] => BLACK
                [tih_leather] => OSCAR
                [bom_dt] => Array
                    (
                        [0] => Array
                            (
                                [mm_spec_fld] => *-*-*-*-*-NO 5, 68 CM-*-*-*-*-*-*-*-*-*-*-*-METAL-*-*-*-*-*-*-*-*-*-*-*-*-BLACK-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
                                [tbid_qnty] => 1
                                [mi_sku] => RHN/208/02 BLACK
                            )

                        [1] => Array
                            (
                                [mm_spec_fld] => *-*-*-*-*-NO 5-*-*-*-*-*-*-*-*-*-*-*-NYLON-*-*-*-*-*-*-*-*-*-*-*-*--*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
                                [tbid_qnty] => 17
                                [mi_sku] => RHN/208/02 BLACK
                            )

                        [2] => Array
                            (
                                [mm_spec_fld] => *-*-*-*-*-NO 3-*-*-*-*-*-*-*-*-*-*-*-NYLON-*-*-*-*-*-*-*-*-*-*-*-*--*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
                                [tbid_qnty] => 8
                                [mi_sku] => RHN/208/02 BLACK
                            )

                        [3] => Array
                            (
                                [mm_spec_fld] => *-*-*-*-*-*-BLG-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-BRANDED ADJUSTER-NA-25MM-NA-NA-NA-NA-PIECES-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
                                [tbid_qnty] => 1
                                [mi_sku] => RHN/208/02 BLACK
                            )

                    )

            )

主阵列的结果将是相应子阵列的公共结果。 你能建议如何使用foreach循环在视图页面上显示它。

2 个答案:

答案 0 :(得分:0)

你想要实现一个嵌套的foreach,正如Raymond Nijland所暗示的那样。

<?php foreach ($items as $item): ?>
<tr>
    <th>SKU</th>
    <td><?php echo escape($item['tih_sku']); ?></td>
...
    <th>BOM DT</th>
    <td>

<?php if (count($item['bom_dt]) > 0): ?>
<?php    foreach ($item['bom_dt'] as $bom_dt): ?>

    //display the bom_dt details

<?php    endforeach; ?>
<?php endif; ?>

    </td>
</tr>
<?php endforeach; ?>

有很多方法可以完成上面的示例,但请查看您正在使用的php框架的文档。如果你没有使用php框架,你绝对应该实现一些东西来逃避你写入页面的数据,以保护自己免受攻击。

答案 1 :(得分:0)

尝试如下

<?php
$var = array(
    array(
        'tih_sku' => '10001',
        'tih_style' => 'RHH/110/02',
        'tih_color' => 'BLACK',
        'tih_leather' => 'PERU',
        'bom_dt' => array(
            array(
                'mm_spec_fld' => 'NO 5, 44.5 CM - METAL - BLACK',
                'tbid_qnty' => '1',
                'mi_sku' => 'RHH/110/04 BLACK',
            ),
            array(
                'mm_spec_fld' => 'NO 3 - NYLON',
                'tbid_qnty' => '7.75',
                'mi_sku' => 'RHH/110/04 BLACK',
            )
        ),
    ),
    array(
        'tih_sku' => '10009',
        'tih_style' => 'RHH/110/04',
        'tih_color' => 'BLACK',
        'tih_leather' => 'PERU',
        'bom_dt' => array(
            array(
                'mm_spec_fld' => 'NO 5, 68 CM - METAL - BLACK',
                'tbid_qnty' => '1',
                'mi_sku' => 'RHN/208/02 BLACK',
            ),
            array(
                'mm_spec_fld' => 'NO 5',
                'tbid_qnty' => '17',
                'mi_sku' => 'RHN/208/02 BLACK',
            )
        ),
    )
);
?>
<?php if(array_filter($var)){ ?>
<style>
table{width:50%;}
td{border:1px solid #999;padding:5px;}
</style>
<table>
    <tr><td>Specification</td><td>Quantity</td><td>Code</td></tr>
<?php
$returnArray = array_map(function($v){ return $v['bom_dt'];},$var);
array_walk($returnArray, function($v2){array_walk($v2, function($v3){
?>
<tr><td><?=$v3['mm_spec_fld'];?></td><td><?=$v3['tbid_qnty'];?></td><td><?=$v3['mi_sku'];?></td></tr>
<?php
});});
?>
</table>
<?php } ?>

demo: https://paiza.io/projects/bhrSiwQi5zxtSMAz7ev0Wg?language=php