如何在 php 中交换数组索引


How to swap array index in php?

$order_total_query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "order_total` WHERE order_id = '" . (int) $order_id . "' ORDER BY sort_order ASC");

如果我打印结果,它会显示:

Array
(
    [0] => Array
        (                                
            [order_id] => 1318
            [code] => shipping
            [title] => UK Shipping  (Weight: 0.00kg)
            [value] => 10.2000
            [sort_order] => 1
        )
    [1] => Array
        (                                
            [order_id] => 1318
            [code] => sub_total
            [value] => 4.7000
            [sort_order] => 3
        )
    [2] => Array
        (                                
            [order_id] => 1318
            [code] => coupon
            [title] => Coupon (10P)
            [value] => -0.4700
            [sort_order] => 4
        )
    [3] => Array
        (                                
            [order_id] => 1318
            [code] => tax
            [title] => VAT (20%)
            [value] => 2.8860
            [sort_order] => 8
        )
    [4] => Array
        (                                
            [order_id] => 1318
            [code] => total
            [title] => Total
            [value] => 17.3160
            [sort_order] => 9
        )
    )

然后

foreach ($order_total_query->rows as $total)
  {
   $text .= $total['title'] . ': ' . html_entity_decode($this->currency->format($total['value'], $order_info['currency_code'], $order_info['currency_value']), ENT_NOQUOTES, 'UTF-8') . "'n";
  }

如果我打印$text,它显示:

Order Totals
UK Shipping  (Weight: 4.00kg): £10.20
Sub-Total: £18.80
coupon : £-0.47
VAT (20%): £5.80
Total: £34.80

但是我想在优惠券不为空时交换小计和优惠券的位置。我需要如下结果:

Order Totals
UK Shipping  (Weight: 4.00kg): £10.20           
coupon : £-0.47
Sub-Total: £18.80
VAT (20%): £5.80
Total: £34.80

试试这个技术。

$temp = $order_total_query[1];
$order_total_query[1] = $order_total_query[2];
$order_total_query[2] = $temp;
print_r($order_total_query)

输出

Array
(
    [0] => Array
        (                                
            [order_id] => 1318
            [code] => shipping
            [title] => UK Shipping  (Weight: 0.00kg)
            [value] => 10.2000
            [sort_order] => 1
        )    
    [1] => Array
        (                                
            [order_id] => 1318
            [code] => coupon
            [title] => Coupon (10P)
            [value] => -0.4700
            [sort_order] => 4
        )
    [2] => Array
        (                                
            [order_id] => 1318
            [code] => sub_total
            [value] => 4.7000
            [sort_order] => 3
        )
    [3] => Array
        (                                
            [order_id] => 1318
            [code] => tax
            [title] => VAT (20%)
            [value] => 2.8860
            [sort_order] => 8
        )
    [4] => Array
        (                                
            [order_id] => 1318
            [code] => total
            [title] => Total
            [value] => 17.3160
            [sort_order] => 9
        )
    )

似乎你需要类似的东西(执行一次(:

UPDATE DB_PREFIX SET sort_order=2 WHERE code = 'coupon'

它会对它们重新排序,但要小心:谁知道sort_order其他地方使用 - 在执行之前检查两次。

此外,您还必须找到sort_order设置为 4(用于优惠券代码(的位置,并将其更改为 2 以用于将来的订单。

定义订单代码索引。

$orderCodes = [
    'sub_shipping' => '',
    'Coupon' => '',
    'sub_total' => '',
    'tax' => '',
    'Total' => ''
];
foreach ($order_total_query->rows as $total) {
    $orderCodes[$total['code']] = $total['title'] . ': ' . html_entity_decode($this->currency->format($total['value'], $order_info['currency_code'], $order_info['currency_value']), ENT_NOQUOTES, 'UTF-8');
}
if (empty($orderCodes['Coupon'])) {
    unset($orderCodes['Coupon'])
}
explode(''n', $orderCodes);