Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

The finished array would look something like this:

array(

'foo bar bar foo' => 10,
'foo' => 10,
'foo bar bar bar foo' => 6,
'foo bar' => 6,
'bar' => 6,
'b' => 5

)

So the values are sorted first by the value, and then by the length of the key.

In MySQL I'd do this:

SELECT product, qty FROM stock ORDER BY qty DESC, LENGTH(product) DESC 

But I'd like to do it in PHP.

share|improve this question
    
also, is there a particular reason you want to do it in PHP? –  Ascherer Dec 21 '11 at 21:09
    
@Ascherer: The data is not present in MySQL, and I want to avoid the transfer time of putting it all in there. My SELECT statement was just an example. –  Drahcir Dec 21 '11 at 21:12
    
ahh, k. That makes sense –  Ascherer Dec 21 '11 at 21:23

3 Answers 3

up vote 3 down vote accepted

Looking at the answers to this question: PHP array multiple sort - by value then by key?, it seems array_multisort is the way to go. (I'm not really sure how array_multisort works, I just kinda hacked this up, and it seems to work).

Try this:

$arr = array(
  'foo bar' => 6,
  'foo' => 10,
  'bar' => 6,
  'b' => 5,
  'foo bar bar bar foo' => 6,
  'foo bar bar foo' => 10
);

array_multisort(array_values($arr), SORT_DESC,
  array_map(create_function('$v', 'return strlen($v);'), array_keys($arr)),
  SORT_DESC, $arr);

Demo: http://codepad.org/mAttNIV7

UPDATE: Added array_map to make it sort by the length of the string, before it was just doing:

$str1 > $str2 instead of strlen($str1) > strlen($str2).

UPDATE 2: In PHP >= 5.3, you can replace create_function with a real anonymous function.

array_map(function($v){return strlen($v);}, array_keys($arr))

Demo 2: http://codepad.viper-7.com/6qrFwj

share|improve this answer
    
hmm, yeah that way works better than mine with the original array, i originally avoided this cause i didnt think it would sort the strings by length, but it looks like it is lol –  Ascherer Dec 21 '11 at 21:24
    
@Ascherer: It was originally sorting the strings by doing $a > $b, it wasn't sorting by length, it just happened to be in that order. I added an array_map to make it sort by legnth. –  Rocket Hazmat Dec 21 '11 at 21:31
    
@Rocket: Thanks for taking time to work this out, you've solved my problem perfectly. –  Drahcir Dec 21 '11 at 21:35
    
@RichardLivingston: You're welcome. Glad I could help. –  Rocket Hazmat Dec 21 '11 at 21:37
    
k, now this way seems messy.... usort makes it cleaner –  Ascherer Dec 21 '11 at 23:23

take a look at php's usort and uasort.

You should be able to define a function that can sort it like that

Not sure if it would work easily with that current array but this one it would

$array = array(
 array('name' => 'foo bar bar foo', 'qty' => 10 ),
 array('name' => 'foo', 'qty' => 6),
 array('name' => 'foo bar bar foo', 'qty' => 6 ),
 array('name' => 'foo bar', 'qty' => 6 )
);

uasort($array, 'arraySort');

function arraySort($a, $b)
{
    if($a['qty'] > $b['qty'])
        return 1;
    elseif($a['qty'] < $b['qty'])
        return -1;
    else
        if(strlen($a['name']) >= strlen($b['name']))
            return 1;
        else
            return -1;
}
share|improve this answer

use the sort function in php. make sure you use the TRUE as the second parameter to preserve the keys.

share|improve this answer
    
sort doesn't have a "preserve keys" parameter. If you want to do that, you'd use asort. –  Rocket Hazmat Dec 21 '11 at 21:03

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.