带有关联整数键的PHP array_multisort

时间:2014-09-16 18:17:35

标签: php arrays associative-array associative array-multisort

我面对以下困境,有这样的数组:

Array ( 
    [10001] => Array ( [wins] => 0 [played] => 1 [coefficient] => 1 ) 
    [10002] => Array ( [wins] => 1 [played] => 1 [coefficient] => 0 ) 
    [10008] => Array ( [wins] => 1 [played] => 1 [coefficient] => 0 ) 
    [10015] => Array ( [wins] => 1 [played] => 1 [coefficient] => 0 ) 
    [10014] => Array ( [wins] => 2 [played] => 2 [coefficient] => 1 ) 
    [10017] => Array ( [wins] => 0 [played] => 2 [coefficient] => 2 )
)

我需要首先按“胜利”排序。然后通过系数'所以在构建像这样的列数组之后我调用了array_multisort:

array_multisort($wins, SORT_DESC, $coeff, SORT_DESC, $standings_data);

很好,一切正常,但是,最终数组中的键是从0到5的索引...我查找了PHP文档并发现:

array_multisort() can be used to sort several arrays at once, or a multi-dimensional
array by one or more dimensions.
Associative (string) keys will be maintained, but numeric keys will be re-indexed.

事实证明我的密钥被解释为数字,因此它们被重新编入索引。显然我不希望这种情况发生,但我无法找到一种方法将它们改为字符串而不实际改变它们的价值,而这是我无法做到的。

深入研究这个问题:有没有办法对一个关联数组进行多重排序来维护它的数字键?

0 个答案:

没有答案