-2

Possible Duplicate:
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 ) 
)

How do i sort these according to their [votes_balance] value?

1
  • this has been asked multiple times before. please use the search function before asking. Commented Oct 26, 2011 at 11:24

1 Answer 1

0

Use the usort function of PHP with something like this:

function compare($a, $b) {
    if($a['id'] == $b['id']) return 0;
    return ($a['id'] < $b['id']) -1 : 1;
}
Sign up to request clarification or add additional context in comments.

2 Comments

too complicated. just do return $a['id'] - $b['id'];.
i actually copied the example code from the php-docs.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.