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

It gives sorting rate array..

M1000B20000
M100B1000
M5000BUnlimited
M500B10000
MUnlimitedBUnlimited

I Need this sorting..

M100B1000
M500B10000
M1000B20000
M5000BUnlimited
MUnlimitedBUnlimited

Give some ideas...

2
  • 1
    do you want it sorted by length of the string or the numeric value inside it? Commented Apr 27, 2012 at 5:38
  • the sort does not take numbers into account, this is just a simple A-Z string sort comparisson, so M100B is not going to be before M1000B (extra 0). You will have to manually sort / reg expression this. Commented Apr 27, 2012 at 5:39

1 Answer 1

1

it looks like you want to sort it based on the size of the string if yes take a look at this example

more explanation here 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>';
1
  • M100B1000, M500B10000, M1000B20000,MUnlimitedBUnlimited M5000BUnlimited, last is change Commented Apr 27, 2012 at 5:37

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.