2017-04-11 101 views
2

我有一个这样的数组在PHP中。我有什么用量排序上升:多维数组排序键值

Array 
(
    [0] => Array 
     (
      [trans_id] => PR58EC68AFE8B0F3186 
      [bill_number] => 1 
      [order_date] => 2017-04-11 
      [amount] => 800 
      [trx_type] => purchase 
     ) 

    [1] => Array 
     (
      [trans_id] => PR58EC68AFE8B0F3186 
      [bill_number] => 1 
      [order_date] => 2017-04-11 
      [amount] => 150 
      [trx_type] => purchase-paid 
     ) 

    [2] => Array 
     (
      [trans_id] => PR58EC68AFE8B0F3186 
      [bill_number] => 1 
      [order_date] => 2017-04-11 
      [amount] => 100 
      [trx_type] => purchase-paid 
     ) 

    [3] => Array 
     (
      [trans_id] => BL58EC68EC4BCA18805 
      [bill_number] => 1 
      [order_date] => 2017-04-11 
      [amount] => 2000 
      [trx_type] => bill 
     ) 
+1

欢迎来到SO。请先到[帮助中心](http://stackoverflow.com/help),然后阅读[如何提问](http://stackoverflow.com/help/how-to-ask)并提供[MCVE :最小,完整和可验证示例](http://stackoverflow.com/help/mcve)。如果身边的人可以轻松地阅读和理解你的意思,或者是什么问题,他们会更愿意帮忙:) – OldPadawan

+1

欢迎来到Stackoverflow。看看http://stackoverflow.com/help/how-to-ask - 正如您所看到的,我们在这里帮助您解决特定的编程问题,而不是为您编写代码。向我们展示迄今为止您尝试过的内容,分享您的想法,我们将帮助您...但是没有人会为您编写代码。 – Twinfriends

+0

[PHP:按值排序数组]的可能重复(http://stackoverflow.com/questions/30813236/php-sort-array-by-value) – xhg

回答

1

可以使用usort()内置的功能与您的自定义功能,这样

<?php 
$yourarray = array(
    0 => array(
     'bill_number' => 3, 
     'amount' => 100 
    ), 
    1 => array(
     'bill_number' => 4, 
     'amount' => 50 
    ), 
    2 => array(
     'bill_number' => 5, 
     'amount' => 150 
    ), 
); 

function sortByOrder($a, $b) 
{ 
    return $a['amount'] - $b['amount']; 
} 

usort($yourarray, 'sortByOrder'); 
echo "<pre>"; 
print_r($yourarray); 
?> 

然后输出将是

Array 
(
    [0] => Array 
     (
      [bill_number] => 4 
      [amount] => 50 
     ) 

    [1] => Array 
     (
      [bill_number] => 3 
      [amount] => 100 
     ) 

    [2] => Array 
     (
      [bill_number] => 5 
      [amount] => 150 
     ) 

) 

更多信息

http://php.net/manual/en/function.usort.php