How to swap array index in php? -


$order_total_query = $this->db->query("select * `" . db_prefix . "order_total` order_id = '" . (int) $order_id . "' order sort_order asc"); 

if print result, shows:

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

after that,

foreach ($order_total_query->rows $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";   } 

if print $text, shows :

order totals uk shipping  (weight: 4.00kg): £10.20 sub-total: £18.80 coupon : £-0.47 vat (20%): £5.80 total: £34.80 

but want interchange position of sub-total , coupon, when coupon not empty.i need result below:

order totals uk shipping  (weight: 4.00kg): £10.20            coupon : £-0.47 sub-total: £18.80 vat (20%): £5.80 total: £34.80 

try technique.

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

output

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

Comments

Popular posts from this blog

Ansible - ERROR! the field 'hosts' is required but was not set -

customize file_field button ruby on rails -

SoapUI on windows 10 - high DPI/4K scaling issue -