php数组键排序与字符串&数字

时间:2012-04-27 05:28:28

标签: php arrays sorting

<?php
$rate=array ( "M100B1000" => "40.00", "M1000B20000" => "80.00", "M500B10000" => "50.00", "MUnlimitedBUnlimited" => "240.00", "M5000BUnlimited" => "120.00 ") ;
ksort($rate);
print_r($rate);
?>

它给出了排序率数组..

M1000B20000
M100B1000
M5000BUnlimited
M500B10000
MUnlimitedBUnlimited

我需要这个排序..

M100B1000
M500B10000
M1000B20000
M5000BUnlimited
MUnlimitedBUnlimited

提出一些想法......

1 个答案:

答案 0 :(得分:1)

看起来您希望根据字符串的大小对其进行排序,如果是,请查看此示例

此处有更多解释http://php.net/manual/en/function.sort.php

 function shortestFirst(key($str1), key($str2)) {

        return strlen(key($str1)) - strlen(key($str2));

     }
    $rate=array ( "M100B1000" => "40.00", "M1000B20000" => "80.00", "M500B10000" => "50.00", "MUnlimitedBUnlimited" => "240.00", "M5000BUnlimited" => "120.00 "); 


    uasort($data, 'shortestFirst');
        print_r($data);
        echo '</pre>';