0

I want to sort this array:

 Array ( [0] => Array ( [qid] => 1_2_qb0 [hendel] => 7.7 ) [1] => Array ( [qid] => 1_2_qb1 [hendel] => 4.13 ) [2] => Array ( [qid] => 1_2_qb2 [hendel] => 6.61 ) [3] => Array ( [qid] => 1_2_qb3 [hendel] => 7.22 ) [4] => Array ( [qid] => 1_2_qb4 [hendel] => 3.13 ) [5] => Array ( [qid] => 1_2_qb5 [hendel] => 6.91 ) [6] => Array ( [qid] => 1_2_qb6 [hendel] => 7.3 ) [7] => Array ( [qid] => 1_2_qb7 [hendel] => 6.65 ) [8] => Array ( [qid] => 1_2_qb8 [hendel] => 6.7 ) [9] => Array ( [qid] => 1_3_qb0 [hendel] => 7.43 ) [10] => Array ( [qid] => 1_3_qb1 [hendel] => 7.7 ) [11] => Array ( [qid] => 1_3_qb2 [hendel] => 7.39 ) [12] => Array ( [qid] => 1_3_qb3 [hendel] => 7.83 ) [13] => Array ( [qid] => 1_3_qb4 [hendel] => 7.26 ) [14] => Array ( [qid] => 1_3_qb5 [hendel] => 7.04 ) [15] => Array ( [qid] => 1_3_qb6 [hendel] => 7.65 ) [16] => Array ( [qid] => 1_3_qb7 [hendel] => 7.43 ) [17] => Array ( [qid] => 1_4_qb0 [hendel] => 7.22 ) [18] => Array ( [qid] => 1_4_qb1 [hendel] => 7.26 ) [19] => Array ( [qid] => 1_4_qb2 [hendel] => 7.43 ) [20] => Array ( [qid] => 1_4_qb3 [hendel] => 8.09 ) ) 

From high to low on "hendel" but the "qid" must be related to the "hendel" value. sorting array's doesn't work. (asort, arsort, ksort, krsort => flags: sort_nummeric)

Printing values without sorting:

1_2_qb0 : 7.7
1_2_qb1 : 4.13
1_2_qb2 : 6.61
1_2_qb3 : 7.22
1_2_qb4 : 3.13
1_2_qb5 : 6.91
1_2_qb6 : 7.3
1_2_qb7 : 6.65
1_2_qb8 : 6.7
1_3_qb0 : 7.43
1_3_qb1 : 7.7
1_3_qb2 : 7.39
1_3_qb3 : 7.83
1_3_qb4 : 7.26
1_3_qb5 : 7.04
1_3_qb6 : 7.65
1_3_qb7 : 7.43
1_4_qb0 : 7.22
1_4_qb1 : 7.26
1_4_qb2 : 7.43
1_4_qb3 : 8.09

with sorting (krsort):

1_4_qb3 : 8.09
1_4_qb2 : 7.43
1_4_qb1 : 7.26
1_4_qb0 : 7.22
1_3_qb7 : 7.43
1_3_qb6 : 7.65
1_3_qb5 : 7.04
1_3_qb4 : 7.26
1_3_qb3 : 7.83
1_3_qb2 : 7.39
1_3_qb1 : 7.7
1_3_qb0 : 7.43
1_2_qb8 : 6.7
1_2_qb7 : 6.65
1_2_qb6 : 7.3
1_2_qb5 : 6.91
1_2_qb4 : 3.13
1_2_qb3 : 7.22
1_2_qb2 : 6.61
1_2_qb1 : 4.13
1_2_qb0 : 7.7
3
  • 1
    It's a mess of code, but I think old friend usort will do the stuff Commented Oct 2, 2013 at 10:40
  • usort($myArray, function($a,$b) {if ($a['hendel'] == $b['hendel']) { return 0; } return ($a['hendel'] < $b['hendel']) ? -1 : 1; }); Commented Oct 2, 2013 at 10:42
  • @MarkBaker I think, then it's return $a['hendel']<$b['hendel']?-1:$a['hendel']!=$b['hendel'] :p Commented Oct 2, 2013 at 10:44

2 Answers 2

1

You can use the usort() function:

usort($array, function(array $a, array $b) {
    if($a['hendel'] == $b['hendel']) {
        return 0;
    }
    return $a['hendel'] < $b['hendel'];
});
Sign up to request clarification or add additional context in comments.

Comments

0

You have a comparison of array sorting functions provided by PHP here : https://www.php.net/manual/en/array.sorting.php. You certainly will find what you need here.

You may use custom rules to sort your array, or use existing sorting functions.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.