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
4

2 回答 2

1

您可以使用以下usort()功能:

usort($array, function(array $a, array $b) {
    if($a['hendel'] == $b['hendel']) {
        return 0;
    }
    return $a['hendel'] < $b['hendel'];
});
于 2013-10-02T10:44:07.530 回答
0

您可以在此处比较 PHP 提供的数组排序功能:http: //fr2.php.net/manual/en/array.sorting.php。你肯定会在这里找到你需要的东西。

您可以使用自定义规则对数组进行排序,或使用现有的排序函数。

于 2013-10-02T10:44:03.560 回答