根据数组的子数组对数组进行排序



可能重复:
基于数组内部键值的数组排序

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]值对它们进行排序?

使用PHP的usort函数,如下所示:

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

相关内容

  • 没有找到相关文章

最新更新