How can I sort a array like this by its keys, from the smaller resolution to the larger one:

$sizes = array(
   '120x120' => 'large',
   '60x60' => 'small',
   '200x200' => 'very large',
   '90x90' => 'medium',
...

?

should be :

  • 60x60
  • 90x90
  • 120x120
  • 200x200
  • ...
share|improve this question

5 Answers

up vote 7 down vote accepted

ksort() in numeric mode should work just fine :

$sizes = array(
   '120x120' => 'large',
   '60x60' => 'small',
   '200x200' => 'very large',
   '90x90' => 'medium',
);

ksort($sizes, SORT_NUMERIC);
var_dump($sizes);

will get you :

array
  '60x60' => string 'small' (length=5)
  '90x90' => string 'medium' (length=6)
  '120x120' => string 'large' (length=5)
  '200x200' => string 'very large' (length=10)


This will work because the size is a numeric -- and is found before the 'x' (not sure what will be done with the 'x' and what follows -- but, anyway, that part of the keys is useless, as it's purely redondant information)

share|improve this answer
+1 that's the way... – Positive Apr 4 '11 at 7:04
thank you, I didn't think sorting by numbers would work because I have a x there :) – Alex Apr 4 '11 at 7:06
@Alexandra PHP will effectively truncate the "number" when it hits the 'x'. As such, you're only really sorting on the first number. – middaparka Apr 4 '11 at 7:08

Try with ksort().

Sorts an array by key, maintaining key to data correlations. This is useful mainly for associative arrays.

Edit: to make the answer complete, please do use the SORT_NUMERIC flag.

share|improve this answer
An explanation would go well with that downvote! – code_burgar Apr 4 '11 at 7:02
1  
-1 That does not work try to understand question output [120x120] => large [200x200] => very large [60x60] => small [90x90] => medium – Positive Apr 4 '11 at 7:03
1  
these are strings and will not get sorted like integers – Positive Apr 4 '11 at 7:04
+1 from me, maybe there vegetarian?... – Lawrence Cherone Apr 4 '11 at 7:06

If you take a look at PHP's Sorting Arrays manual page, you can see there are a few options for sorting the array based on its keys, of which ksort (using the SORT_NUMERIC modifier) is most likely the one you're after.

share|improve this answer
but it doesn't really work. I get 120x120, 200x200, 60x60, 90x90 ... – Alex Apr 4 '11 at 7:03
@Alexandra Apologies - you need to use the SORT_NUMERIC modifier to ksort. @Pascal's superior answer illustrates this perfectly. :-) – middaparka Apr 4 '11 at 7:04

you need natural sort by keys, can use uksort

uksort($array, 'strnatcasecmp');
share|improve this answer
$sizes = array(
   '120x120' => 'large',
   '60x60' => 'small',
   '200x200' => 'very large',
   '90x90' => 'medium');
uksort($sizes, 'userSorting');
print_r($sizes);
function userSorting($a, $b) {
    $a = explode('x', $a);
    $b = explode('x', $b);
    return $a[0] > $b[0];
}
share|improve this answer

Your Answer

 
or
required, but never shown
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.