在 PHP 中合并两个循环并按日期排序


<div class="transaction-list">
<!-- START 1ST LOOP -->
<?php
    foreach($transaction_list_buy as $transaction) {
?> 
    <div class="transaction-item px-4 py-3" data-toggle="modal" data-target="#P<?php echo $transaction->deal_id; ?>">
        <div class="row align-items-center flex-row">
            <div class="col-2 col-sm-1 text-center d-none d-sm-block"> <span class="d-block text-4 font-weight-300"><?php $date=date_create($transaction->deal_datetime); echo date_format($date,"d"); ?></span> <span class="d-block text-1 font-weight-300 text-uppercase"><?php $date=date_create($transaction->deal_datetime); echo date_format($date,"M"); ?></span> </div>
            <div class="col col-sm-7"> <span class="d-block text-4"><?php echo $transaction->deal_id; ?></span> <span class="text-muted">
            <?php 
                    if($transaction->deal_status==0) {
                        echo "Buyer Initiated";
                } else if($transaction->deal_status==1) {
                    echo "Prepay Verified Deal";
                } else if($transaction->deal_status==2) {
                    echo "Seller Confirmed";
                } else if($transaction->deal_status==3) {
                    echo "Buyer Received";
                } else if($transaction->deal_status==4) {
                    echo "Seller Claimed";
                } else if($transaction->deal_status==5) {
                    echo "Prepay Released Payment";
                } else if($transaction->deal_status==6) {
                    echo "Buyer Cancelled";
                }else if($transaction->deal_status==7) {
                    echo "Prepay Dropped";
                }?>
            </span> </div>
            <div class="col-2 col-sm-2 text-center text-3"> <span class="text-success" data-toggle="tooltip" data-original-title="Payment Verified"><i class="fas fa-check-circle"></i></span> </div>
            <div class="col-3 col-sm-2 text-right text-4"> <span class="text-2 text-uppercase">RM</span> <span class="text-nowrap"><?php echo number_format($transaction->payment_amount, 2, '.', ''); ?></span> </div>
        </div>
    </div>
<?php
}
?>
<!-- START 2ND LOOP -->
<?php
    foreach($transaction_list_sell as $transaction) {
?> 
    <div class="transaction-item px-4 py-3" data-toggle="modal" data-target="#P<?php echo $transaction->deal_id; ?>">
        <div class="row align-items-center flex-row">
            <div class="col-2 col-sm-1 text-center d-none d-sm-block"> <span class="d-block text-4 font-weight-300"><?php $date=date_create($transaction->deal_datetime); echo date_format($date,"d"); ?></span> <span class="d-block text-1 font-weight-300 text-uppercase"><?php $date=date_create($transaction->deal_datetime); echo date_format($date,"M"); ?></span> </div>
            <div class="col col-sm-7"> <span class="d-block text-4"><?php echo $transaction->deal_id; ?></span> <span class="text-muted">
            <?php 
                    if($transaction->deal_status==0) {
                        echo "Buyer Initiated";
                } else if($transaction->deal_status==1) {
                    echo "Prepay Verified Deal";
                } else if($transaction->deal_status==2) {
                    echo "Seller Confirmed";
                } else if($transaction->deal_status==3) {
                    echo "Buyer Received";
                } else if($transaction->deal_status==4) {
                    echo "Seller Claimed";
                } else if($transaction->deal_status==5) {
                    echo "Prepay Released Payment";
                } else if($transaction->deal_status==6) {
                    echo "Buyer Cancelled";
                }else if($transaction->deal_status==7) {
                    echo "Prepay Dropped";
                }?>
            </span> </div>
            <div class="col-2 col-sm-2 text-center text-3"> <span class="text-success" data-toggle="tooltip" data-original-title="Payment Verified"><i class="fas fa-check-circle"></i></span> </div>
            <div class="col-3 col-sm-2 text-right text-4"> <span class="text-2 text-uppercase">RM</span> <span class="text-nowrap"><?php echo number_format($transaction->payment_amount, 2, '.', ''); ?></span> </div>
        </div>
    </div>
<?php
}
?>                              
</div>

只是相同的数据结构。 不同的是买入(1st(和卖出(2nd(。

根据当前代码,数据将从完成的第一个循环开始打印,然后第二个循环将开始。 这里的问题是,两者都包含日期,我想合并这两者,并根据顶部的最新/最新日期对它们进行排序。

目前,循环将从最旧的开始。

在输出任何内容之前进行合并。

迭代两个事务列表,并使用日期时间作为键将其项添加到新数组中。您可能需要将$datetime转换为可排序的格式,如 Y-m-d 或时间戳(如果尚未转换为可排序格式(。

foreach ([$transaction_list_buy, $transaction_list_sell] as $list) {
    foreach ($list as $transaction) {
        $datetime = $transaction->deal_datetime;
        // reformat $datetime if it is not sortable
        $merged[$datetime][] = $transaction; // append rather than assign
    }
}

每个日期时间应包含一个事务数组。如果我们设置一个像 $merged[$datetime] = $transaction; 这样的值而不是$merged[$datetime][] = $transaction;那么如果某些事务具有相同的日期时间值,我们可能会覆盖值。

然后您可以按键排序并输出排序列表:

krsort($merged);
foreach ($merged as $datetime => $transactions) {
    foreach ($transactions as $transaction) {
        // output transaction
    }
}

与合并/排序无关,但您可以考虑将状态字符串存储在数组中。然后你可以做

echo $status_string[$transaction->deal_status];

而不是所有其他如果。

没关系。 找到最简单的方法。 只需查询买入或卖出。 然后按日期描述排序

相关内容

  • 没有找到相关文章

最新更新