1

我有一个无法编辑的查询,它在 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 语句,但没有任何想法?

4

1 回答 1

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]);
    }

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

于 2019-01-09T05:59:38.593 回答