根据子阵列对数组进行排序

时间:2011-10-26 11:11:10

标签: php arrays multidimensional-array

  

可能重复:
  sorting array based on inner-array key-value

Array ( 
[0] => Array ( 
    [id] => 2 
    [name] => 4 
    [closed] => 
    [date] => 1319625994 
    [votes_up] => 0 
    [votes_down] => 0 
    [votes_pct_up] => 0 
    [votes_pct_down] => 0 
    [votes_balance] => 0 
    [votes_total] => 0 ) 
[1] => Array ( 
    [id] => 3 
    [name] => 3 
    [closed] => 
    [date] => 1319625994 
    [votes_up] => 0 
    [votes_down] => 0 
    [votes_pct_up] => 0 
    [votes_pct_down] => 0 
    [votes_balance] => 0 
    [votes_total] => 0 ) 
[2] => Array ( 
    [id] => 4 
    [name] => 2 
    [closed] => 
    [date] => 1319625995 
    [votes_up] => 1 
    [votes_down] => 0 
    [votes_pct_up] => 100 
    [votes_pct_down] => 0 
    [votes_balance] => 1 
    [votes_total] => 1 ) 
[3] => Array ( 
    [id] => 1 
    [name] => 1 
    [closed] => 
    [date] => 1319623450 
    [votes_up] => 2 
    [votes_down] => 0 
    [votes_pct_up] => 100 
    [votes_pct_down] => 0 
    [votes_balance] => 2 
    [votes_total] => 2 ) 
)

如何根据[votes_balance]值对其进行排序?

1 个答案:

答案 0 :(得分:0)

使用PHP的usort函数:

function compare($a, $b) {
    if($a['id'] == $b['id']) return 0;
    return ($a['id'] < $b['id']) -1 : 1;
}