如何在php中交换数组索引?

时间:2016-04-19 06:14:02

标签: php arrays

$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

3 个答案:

答案 0 :(得分:0)

尝试这种技巧。

$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
        )
    )

答案 1 :(得分:0)

定义订单代码索引。

$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);

答案 2 :(得分:0)

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

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

它会对它们进行重新排序,但请注意:谁知道sort_order使用的位置 - 在执行之前检查它两次。

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