【问题标题】:Divide Multideminsional Array by key values php and outputing to form将多维数组除以键值php并输出形成
【发布时间】:2019-05-29 11:09:08
【问题描述】:

我有一个无法编辑的查询,它在 if 语句上输出一个数组,并根据变量的值输出到可打印的 html 页面(作为带有分页符的表),但我需要进一步研究数组,以便它根据键的不同值的数量输出多张带分页符的工作表。

if($Guide=='N'){
$decisionN[$j] = array(
   'Qty'=>$Qty ,
'Comments'=>$Notes,
'Location'=>$Location,
'GuideRef'=>$GuideRef ,  
                     );
$j++; 
    }

然后

<?php
}
if ($decisionN){
$Guide2="N"
    ?>

&nbsp;&nbsp; Checkin for <?php echo $Guide2;?>
<!--</div>-->

    <table class="tablesorter" id="guides" style="border: 1px solid black;">

        <thead>
            <tr>

              <!--<th style="width: 15px;">PO</th> -->
                <th style="width: 5px;">Qty</th>
                <th style="width: 15px;">COMMENTS</th>
                <th style="width: 10px;">LOCATION</th>
                <th style="width: 12px;">GUIDEREF</th>
            </tr>
        </thead>
        <tbody>

                <?php foreach($decisionN as $v){
                     echo "<tr>";
                  foreach($v as $vv){
                     echo "<td>{$vv}</td>";
                }
                    echo "<tr>";
}

 ?> </tbody><tfoot> </tfoot> </table></div> </div> <footer>Checkin List</footer>

工作完美,并在下一个指南值的页脚后中断,我需要根据 $GuideRef 将 $decisionN 划分为不同的数组(可能是 $GuideRef 的 10 个不同值,每个 $GuideRef 可能有 100 个“行”)我尝试了几个不同的 foreach 语句,但没有任何想法?

【问题讨论】:

    标签: php arrays html multidimensional-array


    【解决方案1】:

    我想我明白你需要什么。

    你有一个这样的数组:

    [
        [
            'Qty'=>$Qty,
            'Location'=>$Location,
            'GuideRef'=>$GuideRef,
    
        ],
        [
            'Qty'=>$Qty2,
            'Location'=>$Location2,
            'GuideRef'=>$GuideRef,
        ],
        [
            'Qty'=>$Qty3,
            'Location'=>$Location3,
            'GuideRef'=>$GuideRef2,
        ],
    ]
    

    ...但你想要这样

    [
        'guideref1' => [
            [
                'Qty'=>$Qty,
                'Location'=>$Location,
            ],
            [
                'Qty'=>$Qty2,
                'Location'=>$Location2,
            ]
        ],
        'guideref2' => [
            [
                'Qty'=>$Qty3,
                'Location'=>$Location3,
            ]
         ]
    ]
    

    你应该做的是:

    在您的初始循环(您生成数组的那个循环)中,在每次迭代中,检查您是否已经为键“guiderefX”生成了一个子数组,如果您这样做了,则推送到它,否则,创建一个使用键“guiderefX”清空子数组,然后将其推入。

    上面将允许您在显示表格时通过“guideref”创建“组”。

    这是一个示例,其中我按颜色对下面的数组进行分组:

    <?php
        $arr = [
            [
                'color' =>  'yellow',
                'name' => 'apple'
            ],
            [
                'color' =>  'yellow',
                'name' => 'lemon'
            ],
            [
                'color' =>  'green',
                'name' => 'watermelon'
            ]
        ];
    
        $length = count($arr);
        $new_arr = [];
        for ($i=0; $i<$length; $i++) { 
            if(!isset($new_arr[ $arr[$i]['color'] ])) {
                $new_arr[ $arr[$i]['color'] ] = [];
            }
            array_push( $new_arr[ $arr[$i]['color'] ], $arr[$i]);
        }
    

    我希望我明白你需要什么......

    【讨论】:

      猜你喜欢
      • 2012-03-03
      • 2023-04-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-05
      • 1970-01-01
      • 1970-01-01
      • 2014-12-11
      相关资源
      最近更新 更多