2017-02-09 58 views
-2

請讓我知道如何排序數組與一列line_total降序格式。我想要line_total降序格式的數組bcoz我想要顯示數據上限銷售。所以請幫助我。我如何排序數組在PHP中

Array 
(
    [totalusers] => 1 
    [TranReport] => Array 
     (
      [start] => 01-01-2017 
      [end] => 31-12-2017 
     ) 

    [webhits] => 794 
    [paypal] => Yes 
    [cash] => No 
    [Transactions] => Array 
     (
      [0] => Array 
       (
        [order_date] => 03-02-2017 
        [customer_name] => Mohsin khan 
        [payment_method] => PayPal 
        [product_list] => Array 
         (
          [0] => Array 
           (
            [product_name] => USB Cable – Iphone → 1M USB Cable - Iphone 
            [qty] => 1 
            [line_total] => 9 
           ) 

          [1] => Array 
           (
            [product_name] => USB Cable – Iphone → 2M USB Cable - Iphone 
            [qty] => 2 
            [line_total] => 24 
           ) 

         ) 

        [quantity] => 3 
        [order_currency] => USD 
        [order_total] => 48.00$ 
        [new_total] => 48.00 
       ) 

      [1] => Array 
       (
        [order_date] => 09-01-2017 
        [customer_name] => Mohsin khan 
        [payment_method] => PayPal 
        [product_list] => Array 
         (
          [0] => Array 
           (
            [product_name] => AA USB Charger 
            [qty] => 1 
            [line_total] => 15 
           ) 

          [1] => Array 
           (
            [product_name] => Car Charger - Dual USB - Low Profile 
            [qty] => 1 
            [line_total] => 15 
           ) 

          [2] => Array 
           (
            [product_name] => Mister Hose → 20m Mister Hose 
            [qty] => 1 
            [line_total] => 20 
           ) 

         ) 

        [quantity] => 3 
        [order_currency] => USD 
        [order_total] => 50.00$ 
        [new_total] => 50.00 
       ) 

      [2] => Array 
       (
        [order_date] => 07-01-2017 
        [customer_name] => Mohsin khan 
        [payment_method] => PayPal 
        [product_list] => Array 
         (
          [0] => Array 
           (
            [product_name] => Quick Charge V3 - Dual USB - Car Charger 
            [qty] => 1 
            [line_total] => 15 
           ) 

          [1] => Array 
           (
            [product_name] => Car Charger - Dual USB - Low Profile 
            [qty] => 1 
            [line_total] => 15 
           ) 

          [2] => Array 
           (
            [product_name] => Mister Hose → 20m Mister Hose 
            [qty] => 1 
            [line_total] => 20 
           ) 

         ) 

        [quantity] => 1 
        [order_currency] => USD 
        [order_total] => 15.00$ 
        [new_total] => 15.00 
       ) 

     ) 

    [deliveytotal] => 0 
) 
+0

我認爲這會幫助你: http://stackoverflow.com/questions/2699086/sort-multi維數組的價值 –

+0

http://php.net/manual/en/function.array-multisort.php – Mohammad

+0

我並不真正瞭解更低價銷售的含義,而是來自數據庫的這一結果?如果是的話,那麼你可以通過sql來進行排序。 – bugscoder

回答

0

您需要使用usort函數http://php.net/manual/en/function.uksort.php。其餘的就是我的,你想達到什麼樣的解釋 - 在產品的line_total總和排序交易

$array = []; //your array 

function sumLinesTotal($product_list) { 
    return array_sum(array_map($product_list, function($product){ 
     return $product['line_total']; 
    }); 
} 

usort($array['Transactions'], function ($a, $b) { 
    return sumLinesTotal($a) < sumLinesTotal($b); 
});