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

Wenn ich das Ergebnis drucken, Es zeigt:Wie Array-Index in PHP austauschen?

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

Danach,

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"; 
    } 

Wenn ich $text drucken, Es zeigt:

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

Aber ich will Tauschen Sie die Position von Sub-Total und Coupon aus, wenn der Coupon nicht leer ist. Ich brauche das Ergebnis t wie unten:

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

Das Array, das Sie vorgestellt haben, hat keine Schlüssel 'currency_code' und' currency_value', Fehler? – RomanPerekhrest

+0

Währungscode = £ 0.00, Währungswert = £ 1.00 –

Antwort

0

Versuchen Sie diese Technik.

$temp = $order_total_query[1]; 
$order_total_query[1] = $order_total_query[2]; 
$order_total_query[2] = $temp; 

print_r($order_total_query) 

Ausgabe

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

den Index Bestellcode definieren.

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

scheint, dass Sie so etwas wie (execute einmal) benötigen:

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

Es wird sie neu anzuordnen, aber darauf achten: wer weiß, wo sonst sort_order verwendet - es überprüfen zweimal, bevor ausführen.

Auch Sie müssen finden, wo sort_order setzt auf 4 (für Gutschein-Code) und ändern Sie es auch für zukünftige Bestellungen 2.

Verwandte Themen