我有一个以下数组,
$versions = array
(
'0.9.md5' => '/var/www/md5_test/0.9.md5',
'1.0.0.md5' => '/var/www/md5_test/1.0.0.md5',
'1.0.1.md5' => '/var/www/md5_test/1.0.1.md5',
'1.0.2.md5' => '/var/www/md5_test/1.0.2.md5',
'1.0.3.md5' => '/var/www/md5_test/1.0.3.md5',
'1.0.9.1.md5' => '/var/www/md5_test/1.0.9.1.md5',
'1.0.9.10.1.md5' => '/var/www/md5_test/1.0.9.10.1.md5',
'1.0.9.10.md5' => '/var/www/md5_test/1.0.9.10.md5',
'1.1.3.md5' => '/var/www/md5_test/1.1.3.md5',
'1.0.9.2.md5' => '/var/www/md5_test/1.0.9.2.md5',
'1.0.9.3.md5' => '/var/www/md5_test/1.0.9.3.md5',
'1.0.9.8.md5' => '/var/www/md5_test/1.0.9.8.md5',
'1.0.9.9.1.md5' => '/var/www/md5_test/1.0.9.9.1.md5',
'1.0.9.9.md5' => '/var/www/md5_test/1.0.9.9.md5',
'1.0.9.md5' => '/var/www/md5_test/1.0.9.md5',
'1.1.0.md5' => '/var/www/md5_test/1.1.0.md5',
'1.1.1.md5' => '/var/www/md5_test/1.1.1.md5',
'1.1.2.md5' => '/var/www/md5_test/1.1.2.md5',
);
在这个数组中,我想按键排序。我搜索了,
Ex:它的顺序应该是:1.0.9.md5, 1.0.9.1.md5,.. , 1.0.9.10.md5, 1.0.9.10.1.md5
我试过了
ksort($versions);
但我无法得到我想要的东西。
答案 0 :(得分:4)
如果这些是版本号,您需要按版本排序,以便1.0.9.2.md5
出现在1.0.9.10.1.md5
之前,那么您需要基于语义版本控制的自定义排序:
uksort($versions, 'version_compare');
答案 1 :(得分:1)
删除“.md5” - > ksort() - >再次添加“.md5”。
foreach($versions as $key => $value) {
$newKey = str_replace(".md5", "", $key);
$new[$newKey] = $value;
}
ksort($new);
foreach($new as $key => $value) {
$newKey = $key . ".md5";
$result[$newKey]= $value;
}
print_r($result);
<强>结果:强>
Array
(
[0.9.md5] => /var/www/md5_test/0.9.md5
[1.0.0.md5] => /var/www/md5_test/1.0.0.md5
[1.0.1.md5] => /var/www/md5_test/1.0.1.md5
[1.0.2.md5] => /var/www/md5_test/1.0.2.md5
[1.0.3.md5] => /var/www/md5_test/1.0.3.md5
[1.0.9.md5] => /var/www/md5_test/1.0.9.md5
[1.0.9.1.md5] => /var/www/md5_test/1.0.9.1.md5
[1.0.9.10.md5] => /var/www/md5_test/1.0.9.10.md5
[1.0.9.10.1.md5] => /var/www/md5_test/1.0.9.10.1.md5
[1.0.9.2.md5] => /var/www/md5_test/1.0.9.2.md5
[1.0.9.3.md5] => /var/www/md5_test/1.0.9.3.md5
[1.0.9.8.md5] => /var/www/md5_test/1.0.9.8.md5
[1.0.9.9.md5] => /var/www/md5_test/1.0.9.9.md5
[1.0.9.9.1.md5] => /var/www/md5_test/1.0.9.9.1.md5
[1.1.0.md5] => /var/www/md5_test/1.1.0.md5
[1.1.1.md5] => /var/www/md5_test/1.1.1.md5
[1.1.2.md5] => /var/www/md5_test/1.1.2.md5
[1.1.3.md5] => /var/www/md5_test/1.1.3.md5
)